package com.gh.gamecenter;

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

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

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

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

public class CropImageActivity extends ToolBarActivity {

    protected CropImageCustom mCropImageCustom;

    public static final String RESULT_CLIP_PATH = "result_clip_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_RATIO, 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);
        View statusBarView = findViewById(R.id.status_bar);

        mTitleTv.setTextColor(mBlackTheme ? Color.WHITE : Color.BLACK);
        mToolbar.setBackgroundColor(getResources().getColor(mBlackTheme ? R.color.text_28282E : R.color.white));
        statusBarView.setBackgroundColor(getResources().getColor(mBlackTheme ? R.color.text_28282E : R.color.white));

        setNavigationTitle(getString(R.string.title_crop_image));
        setToolbarMenu(R.menu.menu_positive);
        MenuItem menuItem = getMenuItem(R.id.layout_menu_positive);
        TextView menuButton = menuItem.getActionView().findViewById(R.id.menu_answer_post);
        menuButton.setTextColor(getResources().getColor(R.color.theme_font));

        float ratio = getIntent().getFloatExtra(EntranceConsts.KEY_IMAGE_CROP_RATIO, 1F);
        mCropImageCustom.setCropRatio(ratio);

        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, !mBlackTheme);
        DisplayUtils.setStatusBarColor(this, R.color.transparent, !mBlackTheme);
    }

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

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (item.getItemId() == R.id.layout_menu_positive) {
            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();
        }
        return super.onMenuItemClick(item);
    }

    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());
            }
        }
    }
}
