package com.gh.gamecenter.video.videomanager

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.gh.gamecenter.common.base.activity.BaseActivity
import com.gh.gamecenter.common.base.activity.BaseActivity_TabLayout
import com.gh.common.util.*
import com.gh.gamecenter.R
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.core.utils.EmptyCallback
import com.gh.gamecenter.common.utils.PermissionHelper
import com.gh.gamecenter.common.utils.updateStatusBarColor
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.common.entity.SimpleGameEntity
import com.gh.gamecenter.entity.VideoLinkEntity
import com.gh.gamecenter.video.upload.view.UploadVideoActivity
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.internal.utils.PathUtils

class VideoManagerActivity : BaseActivity_TabLayout() {

    private var mVideoLink: VideoLinkEntity? = null
    private var mSimpleGameEntity: SimpleGameEntity? = null

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (REQUEST_CODE_SELECT_VIDEO == requestCode && data != null) {
            val uris = Matisse.obtainResult(data)
            if (uris.size > 0) {
                val videoPath = PathUtils.getPath(this, uris[0]) ?: ""
                val intent = if (mVideoLink != null) {
                    UploadVideoActivity.getIntent(
                        this,
                        videoPath,
                        mVideoLink!!,
                        mSimpleGameEntity,
                        mEntrance,
                        "视频投稿",
                        ""
                    )
                } else {
                    UploadVideoActivity.getIntent(this, videoPath, mEntrance, "视频投稿", "")
                }
                startActivityForResult(intent, REQUEST_CODE_VIDEO)
            }
        }

        if (UploadVideoActivity.RESULT_CODE_VIDEO == resultCode) {
            mViewPager.setCurrentItem(0, false)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        updateStatusBarColor(R.color.background_white, R.color.background_white)
        setNavigationTitle("视频投稿")
        setToolbarMenu(R.menu.menu_text)
        mVideoLink = intent.getParcelableExtra(VideoLinkEntity::class.java.simpleName)
        mSimpleGameEntity = intent.getParcelableExtra(SimpleGameEntity::class.java.simpleName)

        val menuItem = getMenuItem(R.id.menu_text)
        val container = menuItem.actionView?.findViewById<View>(R.id.menu_text)
        val text = container?.findViewById<TextView>(R.id.layout_menu_text)
        text?.text = "上传视频"

        if (mVideoLink != null) {
            onMenuItemClick(menuItem)
        }
        LogUtils.logVideoStreamingUpload("进入视频投稿页", "视频投稿", "", "")
    }

    override fun onMenuItemClick(item: MenuItem): Boolean {
        if (item.itemId == R.id.menu_text) {
            PermissionHelper.checkStoragePermissionBeforeAction(this,
                object : EmptyCallback {
                    override fun onCallback() {
                        MtaHelper.onEvent("我的光环_新", "视频投稿", "上传视频")
                        LogUtils.logVideoStreamingUpload("点击上传视频按钮", "视频投稿", "", "")
                        Matisse.from(this@VideoManagerActivity)
                            .choose(MimeType.ofVideo())
                            .countable(true)
                            .singleChoiceMode(true)
                            .addFilter(GhMatisseVideoFilter())
                            .addApplyFilter(GhMatisseVideoApplyFilter())
                            .showSingleMediaType(true)
                            .forResult(REQUEST_CODE_SELECT_VIDEO)
                    }
                })
        }
        return super.onMenuItemClick(item)
    }

    override fun initTabTitleList(tabTitleList: MutableList<String>) {
        tabTitleList.add("已投稿")
        tabTitleList.add("草稿箱")
    }

    override fun onPageSelected(position: Int) {
        super.onPageSelected(position)
        MtaHelper.onEvent("我的光环_新", "视频投稿", "${mTabTitleList[position]}Tab")
        LogUtils.logVideoStreamingUpload("点击${mTabTitleList[position]}tab", "视频投稿", "", "")
    }

    override fun initFragmentList(fragments: MutableList<Fragment>) {
        fragments.add(VideoVerifyFragment())
        fragments.add(VideoDraftFragment())
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        updateStatusBarColor(R.color.background_white, R.color.background_white)
    }

    companion object {
        const val REQUEST_CODE_SELECT_VIDEO = 111
        const val REQUEST_CODE_VIDEO = 112
        const val REQUEST_CODE_DRAFT = 113
        const val REQUEST_CODE_CLIP = 114

        @JvmStatic
        fun getIntent(context: Context, entrance: String?, path: String?): Intent {
            val intent = Intent(context, VideoManagerActivity::class.java)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, BaseActivity.mergeEntranceAndPath(entrance, path))
            return intent
        }

        @JvmStatic
        fun getVideoManagerBundle(
            linkEntity: VideoLinkEntity,
            simpleGameEntity: SimpleGameEntity,
            entrance: String? = null,
            path: String? = ""
        ): Bundle {
            val bundle = Bundle()
            bundle.putString(EntranceConsts.KEY_PATH, path)
            bundle.putParcelable(VideoLinkEntity::class.java.simpleName, linkEntity)
            bundle.putParcelable(SimpleGameEntity::class.java.simpleName, simpleGameEntity)
            bundle.putString(EntranceConsts.KEY_TO, VideoManagerActivity::class.java.name)
            bundle.putString(EntranceConsts.KEY_ENTRANCE, BaseActivity.mergeEntranceAndPath(entrance, path))
            return bundle
        }

        /**
         * WEB端入口
         *
         * @param link 只用于数据统计
         */
        @JvmStatic
        fun getIntent(
            context: Context,
            linkEntity: VideoLinkEntity,
            entrance: String?,
            path: String?
        ): Intent {
            val intent = Intent(context, VideoManagerActivity::class.java)
            intent.putExtra(VideoLinkEntity::class.java.simpleName, linkEntity)
            intent.putExtra(EntranceConsts.KEY_ENTRANCE, BaseActivity.mergeEntranceAndPath(entrance, path))
            return intent
        }
    }
}