package com.gh.gamecenter.gamecollection.publish

import android.app.Activity
import android.app.Dialog
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.core.utils.EmptyCallback
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.common.utils.PermissionHelper
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.setRootBackgroundDrawable
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.databinding.DialogChooseGameCollectionCoverTypeBinding
import com.gh.gamecenter.gamecollection.publish.GameCollectionEditActivity.Companion.REQUEST_CODE_IMAGE
import com.gh.gamecenter.qa.editor.LocalMediaActivity
import com.halo.assistant.HaloApp

class ChooseGameCollectionCoverTypeDialog : BaseDialogFragment() {

    private lateinit var mBinding: DialogChooseGameCollectionCoverTypeBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return DialogChooseGameCollectionCoverTypeBinding.inflate(layoutInflater, null, false).apply {
            mBinding = this
        }.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        NewLogUtils.logShowGameCollectionCoverTypeDialog()
        mBinding.localUploadContainer.setOnClickListener {
            PermissionHelper.checkStoragePermissionBeforeAction(
                requireContext(),
                object : EmptyCallback {
                    override fun onCallback() {
                        startActivityForResult(
                            LocalMediaActivity.getIntent(
                                requireContext(),
                                LocalMediaActivity.ChooseType.IMAGE,
                                1,
                                "创建游戏单"
                            ), REQUEST_CODE_IMAGE
                        )
                    }
                })
            NewLogUtils.logClickGameCollectionChooseCoverDialog("本地上传")
        }
        mBinding.defaultUploadContainer.setOnClickListener {
            ChooseGameCollectionDefaultCoverDialog.show(requireActivity() as AppCompatActivity, tag ?: "")
            NewLogUtils.logClickGameCollectionChooseCoverDialog("默认封面")
        }
        mBinding.cancelBtn.setOnClickListener {
            dismissAllowingStateLoss()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        (requireActivity() as GameCollectionEditActivity).onActivityDialogResult(requestCode, Activity.RESULT_OK, data)
        if (resultCode == Activity.RESULT_OK) {
            dismissAllowingStateLoss()
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val createDialog = super.onCreateDialog(savedInstanceState)
        createDialog.setCanceledOnTouchOutside(true)
        val window = createDialog.window
        window?.setGravity(Gravity.BOTTOM)
        window?.setWindowAnimations(R.style.community_publication_animation)
        return createDialog
    }

    override fun onStart() {
        super.onStart()
        val width = HaloApp.getInstance().application.resources.displayMetrics.widthPixels
        val height = dialog?.window?.attributes?.height ?: ViewGroup.LayoutParams.WRAP_CONTENT
        dialog?.window?.setLayout(width, height)
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        NewLogUtils.logClickGameCollectionChooseCoverDialog("关闭弹窗")
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        if (::mBinding.isInitialized) {
            mBinding.run {
                root.setRootBackgroundDrawable(R.drawable.background_shape_white_radius_12_top_only)
                line1.setBackgroundColor(R.color.divider.toColor(requireContext()))
                line2.setBackgroundColor(R.color.divider.toColor(requireContext()))
                line3.setBackgroundColor(R.color.divider.toColor(requireContext()))
                localUploadContainer.background =
                    R.drawable.choose_game_collection_cover_type_selector.toDrawable(requireContext())
                defaultUploadContainer.background =
                    R.drawable.choose_game_collection_cover_type_selector.toDrawable(requireContext())
                cancelBtn.background = R.drawable.bg_shape_f5_radius_999.toDrawable(requireContext())
                recommentTv.background = R.drawable.bg_game_collection_cover_tag.toDrawable(requireContext())
                titleTv.setTextColor(R.color.text_subtitleDesc.toColor(requireContext()))
                localUploadTitleTv.setTextColor(R.color.text_title.toColor(requireContext()))
                localUploadDesTv.setTextColor(R.color.text_subtitleDesc.toColor(requireContext()))
                defaultUploadTitleTv.setTextColor(R.color.text_title.toColor(requireContext()))
                defaultUploadDesTv.setTextColor(R.color.text_subtitleDesc.toColor(requireContext()))
                recommentTv.setTextColor(R.color.theme_font.toColor(requireContext()))
                cancelBtn.setTextColor(R.color.text_subtitle.toColor(requireContext()))
            }
        }
    }

    companion object {
        fun show(activity: AppCompatActivity, parentTag: String) {
            ChooseGameCollectionCoverTypeDialog().apply {
                arguments = bundleOf(
                    EntranceConsts.KEY_PARENT_TAG to parentTag
                )
            }.show(activity.supportFragmentManager, ChooseGameCollectionCoverTypeDialog::class.java.name)
        }
    }
}