package com.gh.gamecenter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

import com.gh.gamecenter.common.base.activity.ToolBarActivity;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.common.utils.BitmapUtils;
import com.gh.gamecenter.common.view.CropImageCustom;
import com.gh.gamecenter.common.view.cropbox.CropBoxStyle;
import com.gh.gamecenter.core.utils.DisplayUtils;

import java.io.File;
import java.lang.ref.SoftReference;

/**
 * 裁剪图片
 */
public class CropImageActivity extends ToolBarActivity {

    protected CropImageCustom mCropImageCustom;

    public static final String RESULT_CLIP_PATH = "result_clip_path";

    public static final String RESULT_ORIGINAL_PATH = "result_original_path";

    private SoftReference<Bitmap> reference;

    protected boolean mBlackTheme = false;

    @NonNull
    public static Intent getIntent(Context context, String picturePath, float cropRatio, String entrance) {
        return getIntent(context, picturePath, cropRatio, true, entrance);
    }

    @NonNull
    public static Intent getIntent(Context context, String picturePath, float cropRatio, boolean isBlackTheme, String entrance) {
        return getIntent(context, picturePath, cropRatio, isBlackTheme, -1, entrance);
    }

    @NonNull
    public static Intent getIntent(Context context, String picturePath, float cropRatio, boolean isBlackTheme, @LayoutRes int assistRes, String entrance) {
        Intent intent = new Intent(context, CropImageActivity.class);
        intent.putExtra(EntranceConsts.KEY_PATH, picturePath);
        intent.putExtra(EntranceConsts.KEY_ENTRANCE, entrance);
        intent.putExtra(EntranceConsts.KEY_IMAGE_CROP_STYLE, new CropBoxStyle.Rectangle(cropRatio));
        intent.putExtra(EntranceConsts.KEY_BLACK_THEME, isBlackTheme);
        intent.putExtra(EntranceConsts.KEY_ASSIST_RES, assistRes);
        return intent;
    }

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mBlackTheme = getIntent().getBooleanExtra(EntranceConsts.KEY_BLACK_THEME, false);
        super.onCreate(savedInstanceState);

        mCropImageCustom = findViewById(R.id.cropimage_custom);
        TextView tvCancel = findViewById(R.id.tv_cancel);
        TextView tvSubmit = findViewById(R.id.tv_submit);

        CropBoxStyle boxStyle = getIntent().getParcelableExtra(EntranceConsts.KEY_IMAGE_CROP_STYLE);
        if (boxStyle == null) {
            boxStyle = new CropBoxStyle.Rectangle(1F);
        }
        mCropImageCustom.setCropBoxStyle(boxStyle);

        int assistRes = getIntent().getIntExtra(EntranceConsts.KEY_ASSIST_RES, -1);
        if (assistRes > 0) {
            View view = LayoutInflater.from(this).inflate(assistRes, null, false);
            addAssistView(view);
        }

        DisplayUtils.setLightStatusBar(this, false);
        DisplayUtils.setStatusBarColor(this, com.gh.gamecenter.common.R.color.transparent, false);

        tvCancel.setOnClickListener(v -> finish());

        tvSubmit.setOnClickListener(v -> saveImage());
    }

    protected void saveImage() {
        Intent data = new Intent();
        String clipPath = getCacheDir().getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg";
        mCropImageCustom.savePicture(clipPath);
        data.putExtra(RESULT_CLIP_PATH, clipPath);
        setResult(RESULT_OK, data);
        finish();
    }

    @Override
    public int provideNavigationIcon() {
        return mBlackTheme ? com.gh.gamecenter.common.R.drawable.ic_toolbar_back_white : com.gh.gamecenter.common.R.drawable.ic_bar_back;
    }

    public void addAssistView(View view) {
        mCropImageCustom.addAssistView(view);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (reference != null && reference.get() != null) {
            reference.get().recycle();
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && (reference == null || reference.get() == null)) {
            ImageView imageView = mCropImageCustom.getCropImageZoomView();
            Bitmap bitmap = BitmapUtils.getBitmapByFile(getIntent().getStringExtra(EntranceConsts.KEY_PATH),
                    imageView.getWidth(), imageView.getHeight());
            if (bitmap != null) {
                reference = new SoftReference<>(bitmap);
                imageView.setImageBitmap(reference.get());
            }
        }
    }
}
