package com.danding.yang800.Util;

import android.content.Context;
import android.widget.Toast;

import com.danding.yang800.BaseActivity;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.ShareBoardConfig;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

/**
 * Created by Administrator on 2018/1/4.
 */
public class ShareUtil {
    static UMWeb web;
    static Context ctx;

    public static void share(final BaseActivity context, String url, String title, String des, String imageUrl, int image) {
        ctx = context.getApplicationContext();
        if (GeneralUtil.isWXAppInstalledAndSupported(context)) {
//                    if (StringUtil.getString(goodsUrl)) {
            ShareBoardConfig config = new ShareBoardConfig();
            config.setTitleVisibility(false);
            config.setIndicatorVisibility(false);
            web = new UMWeb(url);
//                        LogUtil.d(goodsadapter.getItem(position).getStoreSkuSn() + "goodsadapter.getItem(position).getStoreSkuSn()");
            web.setTitle(title);
            web.setDescription(des);//描述
//                        UMImage umImage = new UMImage(SelectGoodsActivity.this, goodsadapter.getItem(position).getUrl());
//                        web.setThumb(umImage);
            if (!imageUrl.equals("")) {
                UMImage umImage = new UMImage(context, imageUrl);
                web.setThumb(umImage);
            } else if (image != 0) {
                UMImage umImage = new UMImage(context, image);
                web.setThumb(umImage);
            }
            new ShareAction(context).setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                    .setShareboardclickCallback(new ShareBoardlistener() {
                                                    @Override
                                                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                                                        if (GeneralUtil.isWXAppInstalledAndSupported(context)) {
                                                            new ShareAction(context).setPlatform(share_media).setCallback(umShareListener)
                                                                    .withMedia(web)
                                                                    .share();
                                                        } else {
                                                            ToastUtil.show(context, "未安装微信客户端");
                                                        }
                                                    }

                                                }

                    ).
                    open(config);
//                    }

        } else {
            ToastUtil.show(context, "未安装微信客户端");
        }
    }

    private static UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(ctx, "分享成功了", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(ctx, "分享失败了", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(ctx, "分享取消了", Toast.LENGTH_LONG).show();

        }
    };
}
