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.common.provider.CropImageProviderImpl.Companion.IMAGE_TYPE_GAME_COLLECTION_COVER
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
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.feature.selector.LocalMediaActivity
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.feature.selector.ChooseType
import com.gh.gamecenter.gamecollection.publish.GameCollectionEditActivity.Companion.REQUEST_CODE_IMAGE
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()) {
                startActivityForResult(
                    LocalMediaActivity.getIntent(
                        requireContext(),
                        ChooseType.IMAGE,
                        1,
                        "创建游戏单",
                        IMAGE_TYPE_GAME_COLLECTION_COVER
                    ), 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(com.gh.gamecenter.common.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(com.gh.gamecenter.common.R.drawable.background_shape_white_radius_12_top_only)
                line1.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_divider.toColor(requireContext()))
                line2.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_divider.toColor(requireContext()))
                line3.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_divider.toColor(requireContext()))
                localUploadContainer.background =
                    com.gh.gamecenter.common.R.drawable.bg_choose_option_selector.toDrawable(requireContext())
                defaultUploadContainer.background =
                    com.gh.gamecenter.common.R.drawable.bg_choose_option_selector.toDrawable(requireContext())
                cancelBtn.background =
                    com.gh.gamecenter.common.R.drawable.bg_shape_f5_radius_999.toDrawable(requireContext())
                recommentTv.background = R.drawable.bg_game_collection_cover_tag.toDrawable(requireContext())
                titleTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(requireContext()))
                localUploadTitleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(requireContext()))
                localUploadDesTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(requireContext()))
                defaultUploadTitleTv.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(requireContext()))
                defaultUploadDesTv.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(requireContext()))
                recommentTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(requireContext()))
                cancelBtn.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.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)
        }
    }
}