package com.tsgame.tsgame_niuniu.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import com.jx.jyhd.R;
import com.jx.jyhd.simcpux.Constants;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * 微信官方 SDK 分享工具类
 * 用来发送请求给微信，并且可以接通 WXEntryActivity 的回调
 */
public class WeChatOfficialShareUtil {

    private static WeChatOfficialShareUtil instance;
    private IWXAPI api;

    private WeChatOfficialShareUtil(Context context) {
        api = WXAPIFactory.createWXAPI(context, Constants.APP_ID);
    }

    public static WeChatOfficialShareUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (WeChatOfficialShareUtil.class) {
                if (instance == null) {
                    instance = new WeChatOfficialShareUtil(context);
                }
            }
        }
        return instance;
    }

    public boolean isWeChatInstalled() {
        return api != null && api.isWXAppInstalled();
    }

    /**
     * 分享网页到微信
     */
    public void shareWebPage(Context context, String webpageUrl, String title, String description, int sceneType) {
        if (!isWeChatInstalled()) {
            Toast.makeText(context, "您还未安装微信客户端", Toast.LENGTH_SHORT).show();
            return;
        }

        WXWebpageObject webpage = new WXWebpageObject();
        // 追加随机时间戳参数，强制跳过微信的缩略图缓存
        String finalUrl = webpageUrl;
        if (finalUrl != null && !finalUrl.isEmpty()) {
            String separator = finalUrl.contains("?") ? "&" : "?";
            finalUrl = finalUrl + separator + "t=" + System.currentTimeMillis();
        }
        webpage.webpageUrl = webpageUrl;

        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = description;

        try {
            Bitmap thumbBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.logo6);
            if (thumbBmp != null) {
                Bitmap scaled = Bitmap.createScaledBitmap(thumbBmp, 120, 120, true);
                msg.setThumbImage(scaled);
                thumbBmp.recycle();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "webpage" + System.currentTimeMillis();
        req.message = msg;
        req.scene = sceneType;
        api.sendReq(req);
    }

    /**
     * 分享图片到微信
     */
    public void shareImage(Context context, String imagePath, int sceneType) {
        if (!isWeChatInstalled()) {
            Toast.makeText(context, "您还未安装微信客户端", Toast.LENGTH_SHORT).show();
            return;
        }

        WXImageObject imgObj = new WXImageObject();
        try {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { // Android 7.0及以上即可使用FileProvider
                android.net.Uri contentUri = com.tsgame.tsgame_niuniu.util.FileProviderUtil.getUriForFile(context, new java.io.File(imagePath));
                if (contentUri != null && "content".equals(contentUri.getScheme())) {
                    context.grantUriPermission("com.tencent.mm", contentUri, android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    imgObj.imagePath = contentUri.toString();
                } else {
                    imgObj.imagePath = imagePath;
                }
            } else {
                imgObj.imagePath = imagePath;
            }
        } catch (Exception e) {
            e.printStackTrace();
            imgObj.imagePath = imagePath;
        }

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

        try {
            Bitmap bmp = BitmapFactory.decodeFile(imagePath);
            if (bmp != null) {
                Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 120, 120, true);
                msg.setThumbImage(thumbBmp);
                bmp.recycle();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "img" + System.currentTimeMillis();
        req.message = msg;
        req.scene = sceneType;
        api.sendReq(req);
    }
}
