package com.gh.gamecenter.servers.patch

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.R
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.updateStatusBarColor
import com.gh.gamecenter.databinding.ActivityKaifuPatchBinding
import com.gh.gamecenter.feature.entity.ServerCalendarEntity
import com.gh.gamecenter.gamedetail.fuli.kaifu.ServersCalendarActivity

class PatchKaifuActivity : ToolBarActivity() {

    private var mViewModel: PatchKaifuViewModel? = null

    private var mBinding: ActivityKaifuPatchBinding? = null


    override fun getLayoutId(): Int {
        return R.layout.activity_kaifu_patch
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        updateStatusBarColor(R.color.background_white, R.color.background_white)
        setNavigationTitle("修改开服")

        mViewModel = ViewModelProviders.of(this).get(PatchKaifuViewModel::class.java)
        mViewModel?.initData(
            intent.getParcelableExtra(ServerCalendarEntity.TAG) ?: ServerCalendarEntity(),
            intent.getStringExtra(EntranceConsts.KEY_GAMEID) ?: ""
        )
        mViewModel?.postResult?.observe(this, Observer {
            if (it != null) {
                val intent = Intent()
                intent.putExtra(ServersCalendarActivity.GAME_DETAIL_PATCH_KAIFU_KEY, it)
                setResult(Activity.RESULT_OK, intent)
                finish()
            }
        })

        mBinding = ActivityKaifuPatchBinding.bind(mContentView)
        val entity = mViewModel?.serverEntity
        mBinding?.timeTv?.text = entity?.getFormatTime("yyyy-MM-dd HH:mm")
        mBinding?.nameTv?.text = entity?.getNote()
        mBinding?.remarkTv?.text = entity?.remark
        mBinding?.patchName?.setText(entity?.getNote())
        mBinding?.patchRemark?.setText(entity?.remark)
        mBinding?.noteDelTv?.text = entity?.getNote()
        mBinding?.remarkDelTv?.text = entity?.remark


        mBinding?.patchBtn?.isChecked = true
        mBinding?.patchBtn?.setOnCheckedChangeListener { _, b: Boolean ->
            if (b) mBinding?.deleteBtn?.isChecked = false
        }
        mBinding?.deleteBtn?.setOnCheckedChangeListener { _, b: Boolean ->
            if (b) mBinding?.patchBtn?.isChecked = false
        }

        mBinding?.patchRemark?.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                mBinding?.patchRemark?.hint = ""
            } else {
                mBinding?.patchRemark?.hint = "点击填写"
            }
        }
        mBinding?.patchPost?.setOnClickListener {
            if (!mBinding?.deleteBtn?.isChecked!! == !mBinding?.patchBtn?.isChecked!!) {
                toast("请选择修改类型")
            } else {
                val patchName = mBinding?.patchName?.text.toString()
                val patchRemark = mBinding?.patchRemark?.text.toString()

                if (patchName.isNullOrEmpty()) {
                    toast("内名字不能为空")
                    return@setOnClickListener
                }
                if (mBinding?.patchBtn?.isChecked!!
                    && mViewModel?.serverEntity?.getNote() == patchName
                    && mViewModel?.serverEntity?.remark == patchRemark
                ) {
                    toast("内容没有变化，不能提交")
                    return@setOnClickListener
                }
                DialogHelper.showDialog(
                    this,
                    "提示",
                    if (mBinding?.patchBtn?.isChecked!!) "确定修改开服信息吗？" else "确定删除开服信息吗？",
                    "确定",
                    "返回",
                    { mViewModel?.post(mBinding?.patchBtn?.isChecked!!, patchName, patchRemark) },
                    extraConfig = DialogHelper.Config(centerTitle = true, centerContent = true)
                )
            }
        }
    }

    override fun isAutoResetViewBackgroundEnabled() = true

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        updateStatusBarColor(R.color.background_white, R.color.background_white)
        mBinding?.run {
            root.setBackgroundColor(R.color.background.toColor(this@PatchKaifuActivity))
            kaifuAddItemTitle.root.setBackgroundColor(R.color.title.toColor(this@PatchKaifuActivity))
            patchName.setHintTextColor(R.color.title.toColor(this@PatchKaifuActivity))
            patchName.setTextColor(R.color.title.toColor(this@PatchKaifuActivity))
        }
    }

    companion object {

        fun getIntent(context: Context, serverEntity: ServerCalendarEntity, gameId: String): Intent {
            val intent = Intent(context, PatchKaifuActivity::class.java)
            intent.putExtra(ServerCalendarEntity.TAG, serverEntity)
            intent.putExtra(EntranceConsts.KEY_GAMEID, gameId)
            return intent
        }
    }
}
