package com.gh.gamecenter.qa.article.detail

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.databinding.DialogEditHistoryBinding
import com.halo.assistant.HaloApp
import com.halo.assistant.fragment.user.avatar.ChooseDefaultAvatarDialog
import splitties.views.dsl.core.add
import splitties.views.dsl.core.lParams
import splitties.views.dsl.core.wrapContent


class EditHistoryDialog : BaseDialogFragment() {
    private var mBinding: DialogEditHistoryBinding? = null
    private var mEditHistoryArray: LongArray? = null

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mEditHistoryArray = requireArguments().getLongArray(EntranceConsts.KEY_EDIT_HISTORY)
        mBinding?.run {
            cancelTv.setOnClickListener {
                dismissAllowingStateLoss()
            }
            maskView.goneIf((mEditHistoryArray?.size ?: 0) <= 10)
            if ((mEditHistoryArray?.size ?: 0) > 10) {
                contentContainer.setPadding(0, 0, 8F.dip2px(), 22F.dip2px())
            }
            mEditHistoryArray?.forEach {
                contentContainer.add(
                    getTimeTextView(it),
                    contentContainer.lParams(contentContainer.wrapContent, 22F.dip2px())
                )
            }
        }
    }

    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)
    }

    private fun getTimeTextView(time: Long): TextView = TextView(requireContext()).apply {
        text = TimeUtils.getFormatTime(time, "-yyyy-MM-dd HH:mm:ss")
        gravity = Gravity.CENTER
        setTextColor(R.color.text_subtitle.toColor(requireContext()))
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val createDialog = super.onCreateDialog(savedInstanceState)
        createDialog.setCanceledOnTouchOutside(true)
        val window = createDialog.window
        window?.setGravity(Gravity.CENTER)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return createDialog
    }

    companion object {
        fun show(activity: AppCompatActivity, editHistory: List<Long>) {
            EditHistoryDialog().apply {
                arguments = bundleOf(EntranceConsts.KEY_EDIT_HISTORY to editHistory.toLongArray())
            }.show(activity.supportFragmentManager, ChooseDefaultAvatarDialog::class.java.name)
        }
    }
}