package com.gh.gamecenter.qqmini;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Keep;

import com.tencent.connect.share.QzoneShare;
import com.tencent.qqmini.minigame.opensdk.share.OpenSdkShareHelper;
import com.tencent.qqmini.sdk.MiniSDK;
import com.tencent.qqmini.sdk.annotation.ProxyService;
import com.tencent.qqmini.sdk.launcher.core.proxy.ShareProxy;
import com.tencent.qqmini.sdk.launcher.model.ShareData;
import com.tencent.qqmini.sdk.launcher.ui.MoreItem;
import com.tencent.qqmini.sdk.launcher.ui.MoreItemList;
import com.tencent.qqmini.sdk.manager.LoginManager;
import com.tencent.qqmini.sdk.widget.MiniToast;

import java.util.ArrayList;

@Keep
@ProxyService(proxy = ShareProxy.class)
public class ShareProxyImpl extends ShareProxy {

    private static final String TAG = "ShareProxyImpl";

    // 扩展按钮的ID需要设置为[100, 200]这个区间中的值，否则，添加无效。
    public static final int OTHER_MORE_ITEM_1 = 101;
    public static final int OTHER_MORE_ITEM_2 = 102;

    private final OpenSdkShareHelper mShareHelper = new OpenSdkShareHelper();

    /**
     * 获取默认的分享目标，即默认的宿主分享渠道。
     */
    @Override
    public int getDefaultShareTarget() {
        int loginType = LoginManager.getInstance().getOpenSdkLoginInfo().getLoginType();
        switch (loginType) {
            case MiniSDK.LoginType.LOGIN_FROM_WX:
                return ShareData.ShareTarget.WECHAT_FRIEND;
            default:
                return ShareData.ShareTarget.QQ;
        }
    }

    /**
     * 在share之前回调
     * 可以用于决定分享的target是否可进行分享行为
     */
    @Override
    public boolean isShareTargetAvailable(Context context, int shareTarget) {
        return mShareHelper.isShareTargetAvailable(context, shareTarget);
    }

    @Override
    public ArrayList<MoreItem> createMoreItems(MoreItemList.Builder builder) {
        // 自行调整顺序。不支持删除关于，举报。
        builder
                .addShareQQ("QQ", com.tencent.qqmini.sdk.R.drawable.mini_sdk_channel_qq)
                .addShareQzone("QQ空间", com.tencent.qqmini.sdk.R.drawable.mini_sdk_channel_qzone)
                .addShareWxFriends("微信好友", com.tencent.qqmini.sdk.R.drawable.mini_sdk_channel_wx_friend)
                .addShareWxMoments("微信朋友圈", com.tencent.qqmini.sdk.R.drawable.mini_sdk_channel_wx_moment)
                .addRestart("重启小程序", com.tencent.qqmini.sdk.R.drawable.mini_sdk_restart_miniapp)
                .addSettings("设置", com.tencent.qqmini.sdk.R.drawable.mini_sdk_about);
        return builder.build();
    }

    /**
     * 分享
     * @param activity
     * @param shareData 分享数据
     */
    @Override
    public void share(Activity activity, ShareData shareData) {
        switch (shareData.shareTarget) {
            case ShareData.ShareTarget.QQ:
            case ShareData.ShareTarget.SHARE_CHAT:
            case ShareData.ShareTarget.QQ_DIRECTLY:
            case ShareData.ShareTarget.FRIEND_LIST:
                mShareHelper.shareToQQ(activity, shareData);
                break;

            case ShareData.ShareTarget.QZONE:
                mShareHelper.shareToQZone(activity, shareData);
                break;

            case ShareData.ShareTarget.WECHAT_FRIEND:
                mShareHelper.shareToWxSession(activity, shareData);
                break;

            case ShareData.ShareTarget.WECHAT_MOMENTS:
                mShareHelper.shareToWxTimeline(activity, shareData);
                break;
            default:
                break;
        }

        if (MoreItem.isValidExtendedItemId(shareData.shareTarget)) {
            shareToOther(activity, shareData);
        }
    }

    /**
     * 启动第三方分享结果的返回
     * 这里wx分享不会回调这里，需要渠道自己在IWXAPIEventHandler#onResp根据rsp处理
     */
    @Override
    public void onShareActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "requestCode=" + requestCode + ",resultCode=" + resultCode);
        mShareHelper.onShareActivityResult(requestCode, resultCode, data);
    }

    /**
     * 调用第三方分享
     */
    public void shareToOther(Activity activity, ShareData shareData) {
        switch (shareData.shareItemId) {
            case OTHER_MORE_ITEM_1:
                if (shareData.shareInMiniProcess) {
                    activity.runOnUiThread(() -> MiniToast.makeText(activity, "模拟第三方实现", Toast.LENGTH_LONG).show());
                    shareToOtherItem1(activity, shareData);
                }
                break;
            case OTHER_MORE_ITEM_2:
            default:
                MiniToast.makeText(activity, "待第三方实现", Toast.LENGTH_LONG).show();
                break;
        }
    }

    private void shareToOtherItem1(Activity activity, ShareData shareData) {
        Bundle params = new Bundle();
        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT );
        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, shareData.title);//必填
        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, shareData.summary);//选填
        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, shareData.targetUrl);//必填
        ArrayList<String> imageUrlList = new ArrayList<>();
        imageUrlList.add(shareData.sharePicPath);
        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrlList);
    }
}