package com.jx.jyhd.simcpux.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.widget.Toast;

import com.tagmae.tsgame_erwang.utils.Base64ImageUtil;
import com.tsgame.tsgame_niuniu.util.WeChatIntentShareUtil;

/**
 * 微信分享工具类（已代理至无SDK版 WeChatIntentShareUtil）
 */
public class WeChatShareUtil {

    private static WeChatShareUtil instance;
    private final Context mContext;
    private WeChatShareCallback mCallback;

    private WeChatShareUtil(Context context) {
        this.mContext = context.getApplicationContext();
    }

    public static WeChatShareUtil getInstance(Context context) {
        if (instance == null && context != null) {
            instance = new WeChatShareUtil(context);
        }
        return instance;
    }

    public boolean isWeChatInstalled() {
        return WeChatIntentShareUtil.getInstance(mContext).isWeChatInstalled();
    }

    public void setShareCallback(WeChatShareCallback callback) {
        this.mCallback = callback;
    }

    public WeChatShareCallback getShareCallback() {
        return mCallback;
    }

    private void bindCallback() {
        if (mCallback != null) {
            WeChatIntentShareUtil.getInstance(mContext).setShareCallback(new WeChatIntentShareUtil.WeChatShareCallback() {
                @Override
                public void onSuccess() {
                    if (mCallback != null) mCallback.onSuccess();
                }

                @Override
                public void onError(int code, String message) {
                    if (mCallback != null) mCallback.onError(code, message);
                }

                @Override
                public void onCancel() {
                    if (mCallback != null) mCallback.onCancel();
                }
            });
        }
    }

    public void shareWebPage(Activity activity, String webpageUrl, String title, String description, int scene) {
        bindCallback();
        WeChatIntentShareUtil.getInstance(activity).shareWebPage(activity, webpageUrl, title, description, scene);
    }

    public void shareImage(Activity activity, String imagePath, int scene) {
        if (!isWeChatInstalled()) {
            Toast.makeText(activity, "请先安装微信客户端", Toast.LENGTH_SHORT).show();
            if (mCallback != null) {
                mCallback.onError(-1, "未安装微信客户端");
            }
            return;
        }

        Uri imageFile = Base64ImageUtil.base64ToImageFile(activity, imagePath, null);

        if (imageFile == null) {
            if (imagePath.startsWith("data:image/") || imagePath.startsWith("base64,")) {
                com.tagmae.tsgame_erwang.utils.ImageProcessUtils.saveBase64ImageToCache(
                        activity,
                        imagePath,
                        new com.tagmae.tsgame_erwang.utils.ImageProcessUtils.ImageSaveCallback() {
                            @Override
                            public void onSuccess(String savedImagePath) {
                                shareImageInternal(activity, savedImagePath, scene);
                            }

                            @Override
                            public void onError(String errorMsg) {
                                Toast.makeText(activity, errorMsg, Toast.LENGTH_SHORT).show();
                                if (mCallback != null) {
                                    mCallback.onError(-2, errorMsg);
                                }
                            }
                        }
                );
                return;
            } else {
                if (mCallback != null) {
                    mCallback.onError(-2, "图片文件不存在");
                }
                return;
            }
        }

        shareImageInternal(activity, imageFile.getPath(), scene);
    }

    private void shareImageInternal(Activity activity, String imagePath, int scene) {
        bindCallback();
        WeChatIntentShareUtil.getInstance(activity).shareImage(activity, imagePath, scene);
    }

    public void shareBitmap(Activity activity, Bitmap bitmap, int scene) {
        if (bitmap == null) {
            Toast.makeText(activity, "分享图片为空", Toast.LENGTH_SHORT).show();
            if (mCallback != null) {
                mCallback.onError(-1, "分享图片为空");
            }
            return;
        }

        com.tagmae.tsgame_erwang.utils.ImageProcessUtils.saveBitmapToCache(
                activity,
                bitmap,
                new com.tagmae.tsgame_erwang.utils.ImageProcessUtils.ImageSaveCallback() {
                    @Override
                    public void onSuccess(String imagePath) {
                        shareImageInternal(activity, imagePath, scene);
                    }

                    @Override
                    public void onError(String errorMsg) {
                        Toast.makeText(activity, errorMsg, Toast.LENGTH_SHORT).show();
                        if (mCallback != null) {
                            mCallback.onError(-2, errorMsg);
                        }
                    }
                }
        );
    }

    public interface WeChatShareCallback {
        void onSuccess();
        void onError(int code, String message);
        void onCancel();
    }
}
