package com.zxhl.cms.umeng;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.Toast;

import com.zxhl.cms.common.Constant;
import com.zxhl.cms.net.ApiClient;
import com.zxhl.cms.net.SettingPreference;
import com.zxhl.cms.net.callback.BaseObserver;
import com.zxhl.cms.net.model.uc.CoinEntity;
import com.zxhl.cms.umeng.callback.UmengShareCallback;
import com.zxhl.cms.utils.RxBus;
import com.zxhl.cms.utils.Utils;
import com.zxhl.cms.utils.cache.UserCacheUtils;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import io.reactivex.schedulers.Schedulers;

/**
 * Created by gaoleichao on 2018/5/7.
 */

public class UmengShareUtils {

    public static String type = "";
    public static final int NEWS_SHARE = 1;
    public static final int INVITE_SHARE = 2;
    public static final int AWAKE_SHARE = 3;
    public static final int AD_SHARE = 4;
    public static String uid;
    public static String sid;
    public static int mShareType;

    public static void toNewsShare(Activity activity) {

    }


    public static void toShare(Activity activity, int share_type, SHARE_MEDIA share_media, String url, String title, String id, String icon) {
        toShare(activity, share_type, share_media, url, title, id, icon, "");
    }

    public static void toShare(Activity activity, int share_type, SHARE_MEDIA share_media, String url, String title, String id, String icon, String desc) {
        PlatformConfig.setWeixin(Constant.Key.WX_SHARE_APP_ID, Constant.Key.WX_SHARE_KEY);

        if (share_media == null || TextUtils.isEmpty(url) || TextUtils.isEmpty(title)) {
            Toast.makeText(activity, "请稍后，正在准备分享素材;", Toast.LENGTH_SHORT).show();
            RxBus.get().post(Constant.UPDATE_SHAREDATA, true);
            return;
        }
        mShareType = share_type;
        uid = id;
        if (share_media == SHARE_MEDIA.QQ) {
            if (!UMShareAPI.get(activity).isInstall(activity, SHARE_MEDIA.QQ)) {
                Utils.showToast(activity, "未安装QQ");
                return;
            }
            if (TextUtils.isEmpty(Constant.Key.QQ_SHARE_APP_ID)) {
                Utils.showToast(activity, "暂不支持qq分享");
                return;
            }
            sid = "275";
            type = "6";
        } else if (share_media == SHARE_MEDIA.QZONE) {
            if (!UMShareAPI.get(activity).isInstall(activity, SHARE_MEDIA.QQ)) {
                Utils.showToast(activity, "未安装QQ");
                return;
            }
            if (TextUtils.isEmpty(Constant.Key.QQ_SHARE_APP_ID)) {
                Utils.showToast(activity, "暂不支持qq分享");
                return;
            }
            sid = "276";
            type = "5";
        } else if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
            if (!UMShareAPI.get(activity).isInstall(activity, SHARE_MEDIA.WEIXIN)) {
                Utils.showToast(activity, "未安装微信");
                return;
            }
            sid = "274";
            type = "4";
        } else if (share_media == SHARE_MEDIA.WEIXIN) {
            if (!UMShareAPI.get(activity).isInstall(activity, SHARE_MEDIA.WEIXIN)) {
                Utils.showToast(activity, "未安装微信");
                return;
            }
            sid = "277";
            type = "7";
        }
        ShareAction shareAction = new ShareAction(activity).setPlatform(share_media);// 传入平台
        UMImage image;// 网络图片
        if (share_type == UmengShareUtils.NEWS_SHARE && !TextUtils.isEmpty(UserCacheUtils.getMiniProgramImageUrl())) {
            image = new UMImage(activity, icon);
            shareAction.withMedia(image).setCallback(callback).share();
        } else if (share_type == UmengShareUtils.INVITE_SHARE && !TextUtils.isEmpty(UserCacheUtils.getInviteImageUrl())) {
            image = new UMImage(activity, UserCacheUtils.getInviteImageUrl());
            shareAction.withMedia(image).setCallback(callback).share();
        } else {
            image = new UMImage(activity, icon);
            UMWeb umWeb = new UMWeb(url);
            if (share_media == SHARE_MEDIA.WEIXIN_CIRCLE) {
                umWeb.setTitle(title);
            } else {
                umWeb.setTitle(title);
                if (share_type == UmengShareUtils.AD_SHARE) {
                    umWeb.setDescription(desc);
                } else {
                    umWeb.setDescription(SettingPreference.getShareTitle());
                }
            }
            umWeb.setThumb(image);
            shareAction.withMedia(umWeb).setCallback(callback).share();
        }

    }

    public static UmengShareCallback callback = new UmengShareCallback() {
        @Override
        public void onSuccess(SHARE_MEDIA share_media) {
            if (mShareType == INVITE_SHARE) {
                saveTask();
            }
        }

        @Override
        public void onCancel() {
            if (TextUtils.equals(type, "4") || TextUtils.equals(type, "7")) {
                return;
            }
            if (mShareType == INVITE_SHARE) {
                saveTask();
            }

        }

        @Override
        public void onFailure() {
        }
    };


    public static void saveTask() {
        if (TextUtils.isEmpty(type)) {
            return;
        }
        if (TextUtils.equals("4", uid)) {
            return;
        }
       
    }

}
