package com.gh.gamecenter.video.poster

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.R
import com.gh.gamecenter.video.poster.video.VideoPosterFragment
import java.io.File

class PosterClipActivity : ToolBarActivity() {
    override fun getLayoutId(): Int {
        return R.layout.activity_poster_clip
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setNavigationTitle("剪辑封面")

        val videoPath = intent.getStringExtra(EntranceConsts.KEY_PATH_VIDEO)
        val fragment =
            supportFragmentManager.findFragmentByTag(VideoPosterFragment::class.java.name) as? VideoPosterFragment
                ?: VideoPosterFragment.newInstance(videoPath, null)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.layout_fragment_content, fragment, VideoPosterFragment::class.java.name)
            .commitAllowingStateLoss()

        findViewById<View>(R.id.menu_next).setOnClickListener {
            val clipPath = cacheDir.absolutePath + File.separator + System.currentTimeMillis() + ".jpg"
            fragment.savePicture(clipPath)

            val intent = Intent()
            intent.putExtra(EntranceConsts.KEY_PATH_VIDEO, videoPath)
            intent.putExtra(EntranceConsts.KEY_POSTER_PATH, clipPath)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }

        DisplayUtils.transparentStatusBar(this)
    }

    override fun provideNavigationIcon(): Int {
        return R.drawable.ic_toolbar_back_white
    }

    override fun handleBackPressed(): Boolean {
        DialogHelper.showDialog(this, "提示", "确定放弃剪辑封面吗？", "确定", "暂不", { finish() })

        return true
    }

    companion object {
        fun getIntent(context: Context, videoPath: String): Intent {
            val intent = Intent(context, PosterClipActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_PATH_VIDEO, videoPath)
            return intent
        }
    }
}