package com.android.chmo.ui.activity.promote;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.android.chmo.R;
import com.android.chmo.app.ChmoApplication;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.HttpApi;
import com.android.chmo.ui.dialog.ShareDialog;
import com.android.chmo.wxapi.WeChatManager;
import com.google.zxing.EncodeHintType;
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 net.glxn.qrgen.android.QRCode;

import butterknife.BindView;
import butterknife.OnClick;

public class ShareActivity extends BaseActivity implements ShareDialog.OnShareListener {

    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;

    private ShareDialog shareDialog;
    private IWXAPI      iwxapi;

    @Override
    protected int getContentResId() {
        return R.layout.activity_share;
    }

    @Override
    protected int getStatusBarStyle() {
        return STATUS_BAR_STYLE_LIGHT_CONTENT;
    }

    @Override
    public void initView() {
        ivQrCode.post(() -> ivQrCode.setImageBitmap(QRCode.from(HttpApi.BASE_URL + "share.jsp?type=1&code=" + ChmoApplication.getApp().getLoginUser().qrcode).withSize(512, 512).withHint(EncodeHintType.MARGIN, 0).bitmap()));
        shareDialog = new ShareDialog(this);
        shareDialog.setOnShareListener(this);
        WeChatManager weChatManager = WeChatManager.getInstance();
        weChatManager.init(this);
        iwxapi = weChatManager.getApi();
    }

    @OnClick({R.id.back, R.id.btn_invite})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.btn_invite:
                shareDialog.show();
                break;
        }
    }

    @Override
    public void onShareWechatSession() {
        WXWebpageObject webpageObject = new WXWebpageObject(HttpApi.BASE_URL + "share.jsp?type=1&code=" + ChmoApplication.getApp().getLoginUser().qrcode);
        WXMediaMessage message = new WXMediaMessage();
        message.mediaObject = webpageObject;
        message.title = "千模通告";
        message.description = "模特，网红，空姐，校花，邀您一键视频验证";
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        message.setThumbImage(bmp);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.scene = SendMessageToWX.Req.WXSceneSession;
        req.message = message;
        boolean success = iwxapi.sendReq(req);
        Log.d("success", success + "");
    }

    @Override
    public void onShareWechatTimeline() {
        WXWebpageObject webpageObject = new WXWebpageObject(HttpApi.BASE_URL + "share.jsp?type=1&code=" + ChmoApplication.getApp().getLoginUser().qrcode);
        WXMediaMessage message = new WXMediaMessage();
        message.mediaObject = webpageObject;
        message.title = "千模——模特，网红，空姐，校花，邀您一键视频验证";
        message.description = "模特，网红，空姐，校花，邀您一键视频验证";
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        message.setThumbImage(bmp);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        req.message = message;
        iwxapi.sendReq(req);
    }
}
