package com.gh.gamecenter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.common.util.MessageShareUtils;
import com.gh.common.util.QRCodeUtils;
import com.gh.gamecenter.common.base.activity.ToolBarActivity;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.common.utils.ImageUtils;
import com.gh.gamecenter.feature.entity.ConcernEntity;

import java.io.File;

/**
 * Created by khy on 2016/11/7.
 * 分享卡片
 */
public class ShareCardActivity extends ToolBarActivity {

    TextView mShareContentTv;
    TextView mShareGameNameTv;
    SimpleDraweeView mShareGameIconDv;
    ImageView mShareQrCodeDv;
    LinearLayout mShareScreenshotLl;
    View mActionbar;
    LinearLayout mShareBottomLl;
    String gameName;
    String gameIconUrl;
    String shareContent;
    String picName;
    String newsId;
    private Handler handler = new Handler();

    @NonNull
    public static Intent getIntent(Context context, ConcernEntity concernEntity, String shareContent) {
        Intent intent = new Intent(context, ShareCardActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(EntranceConsts.KEY_GAMENAME, concernEntity.getGameName());
        bundle.putString(EntranceConsts.KEY_GAME_ICON_URL, concernEntity.getGameIcon());
        bundle.putString(EntranceConsts.KEY_SHARECONTENT, shareContent);
        if (concernEntity.getLink() == null) {
            bundle.putString(EntranceConsts.KEY_NEWSID, concernEntity.getId());
        }
        intent.putExtras(bundle);
        intent.putExtra(EntranceConsts.KEY_ENTRANCE, "(资讯:关注[2-4])");
        return intent;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sharecard;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mShareContentTv = findViewById(R.id.sharecard_content);
        mShareGameNameTv = findViewById(R.id.sharecard_game_name);
        mShareGameIconDv = findViewById(R.id.sharecard_game_icon);
        mShareQrCodeDv = findViewById(R.id.sharecard_qrcode);
        mShareScreenshotLl = findViewById(R.id.sharecard_screenshot);
        mActionbar = findViewById(R.id.normal_toolbar_container);
        mShareBottomLl = findViewById(R.id.sharecard_bottom);

        Bundle extras = getIntent().getExtras();
        gameName = extras.getString(EntranceConsts.KEY_GAMENAME);
        gameIconUrl = extras.getString(EntranceConsts.KEY_GAME_ICON_URL);
        shareContent = extras.getString(EntranceConsts.KEY_SHARECONTENT);
        newsId = extras.getString(EntranceConsts.KEY_NEWSID);

        picName = "shareImg.jpg";

        setNavigationTitle(getString(R.string.title_share_card));

        //修改沉浸栏以及ActionBar 颜色
        mActionbar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.black));

        mShareGameNameTv.setText(gameName);
        mShareContentTv.setText(Html.fromHtml(shareContent));
//        mShareGameIconDv.setImageURI(gameIconUrl);
        ImageUtils.display(mShareGameIconDv, gameIconUrl);

        // 延迟操作，等待截图部分绘制完成
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mShareScreenshotLl.setDrawingCacheEnabled(true);
                mShareScreenshotLl.buildDrawingCache();
                Bitmap drawingCache = mShareScreenshotLl.getDrawingCache();
                saveBitmap(drawingCache);
                MessageShareUtils.getInstance(ShareCardActivity.this).showShareWindows(ShareCardActivity.this, mShareBottomLl, drawingCache, picName, 0);
            }
        }, 200);

        String qrBody;
        if (!TextUtils.isEmpty(newsId)) {
            qrBody = "https://www.ghzs666.com/article/" + newsId + ".html?source=appshare200";
        } else {
            qrBody = "https://www.ghzs.com/?source=appshare200";
        }
        QRCodeUtils.setQRCode(this, qrBody, mShareQrCodeDv);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            builder.detectFileUriExposure();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    public void saveBitmap(Bitmap bm) {
        File file = new File(MessageShareUtils.getSaveBitmapBasePath(this));
        if (!file.isDirectory()) {
            file.delete();
            file.mkdirs();
        }
        if (!file.exists()) {
            file.mkdirs();
        }
        MessageShareUtils.getInstance(ShareCardActivity.this).writeBitmap(file.getPath(), picName, bm, false);
    }

}
