package com.gh.gamecenter.personalhome.background

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.widget.ImageView
import com.gh.common.provider.CropImageProviderImpl.Companion.IMAGE_TYPE_PERSONAL_BACKGROUND
import com.gh.gamecenter.CropImageActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.base.fragment.WaitingDialogFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.BitmapUtils
import com.gh.gamecenter.common.view.cropbox.CropBoxStyle
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.ActivityBackgroundClipBinding
import java.io.File
import java.lang.ref.SoftReference

/**
 * 裁剪背景
 */
class BackgroundClipActivity : BaseActivity() {

    private lateinit var mBinding: ActivityBackgroundClipBinding
    private var reference: SoftReference<Bitmap>? = null
    private var mPostDialog: WaitingDialogFragment? = null

    override fun getLayoutId(): Int = R.layout.activity_background_clip

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        DisplayUtils.transparentStatusBar(this)
        mBinding = ActivityBackgroundClipBinding.bind(mContentView)

        mBinding.cancelTv.setOnClickListener {
            finish()
        }
        mBinding.nextTv.setOnClickListener {
            mPostDialog = WaitingDialogFragment.newInstance("正在生成预览...")
            mPostDialog?.show(supportFragmentManager, null)
            runOnIoThread {
                val data = Intent()
                val clipPath = cacheDir.absolutePath + File.separator + System.currentTimeMillis() + ".jpg"
                mBinding.cropImageIv.savePicture(clipPath)

                data.putExtra(CropImageActivity.RESULT_CLIP_PATH, clipPath)
                setResult(Activity.RESULT_OK, data)
                mPostDialog?.dismiss()
                finish()
            }

        }
    }


    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus && (reference == null || reference?.get() == null)) {
            val imageView: ImageView = mBinding.cropImageIv.cropImageZoomView
            val bitmap = BitmapUtils.getBitmapByFile(
                intent.getStringExtra(EntranceConsts.KEY_PATH),
                imageView.width, imageView.height
            )
            if (bitmap != null) {
                reference = SoftReference(bitmap)
                imageView.setImageBitmap(reference?.get())
            }
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        if (reference != null && reference!!.get() != null) {
            reference?.get()?.recycle()
        }
    }

    companion object {
        fun getIntent(context: Context, picturePath: String, entrance: String = ""): Intent? {
            val intent = Intent(context, BackgroundClipActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_PATH, picturePath)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, entrance)
            intent.putExtra(EntranceConsts.KEY_IMAGE_CROP_STYLE, CropBoxStyle.Rectangle(356F / 328))
            intent.putExtra(EntranceConsts.KEY_IMAGE_TYPE, IMAGE_TYPE_PERSONAL_BACKGROUND)
            return intent
        }
    }
}