package com.gh.gamecenter.gamedetail.fuli.kaifu

import android.app.Activity
import android.graphics.Paint
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.gh.common.util.CheckLoginUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.layoutInflater
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.databinding.DialogServersCalendearDetailItemBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.MeEntity
import com.gh.gamecenter.feature.entity.ServerCalendarEntity
import com.gh.gamecenter.servers.add.AddKaiFuActivity
import com.gh.gamecenter.servers.patch.PatchKaifuActivity
import com.lightgame.utils.Utils

class ServersCalendarDetailAdapter(
    private val fragment: Fragment,
    private val viewModel: ServersCalendarDetailViewModel,
    private val parentViewModel: ServersCalendarViewModel
) : ListAdapter<ServerCalendarEntity, ServersCalendarDetailAdapter.ServersCalendarDetailViewHolder>(
    object : DiffUtil.ItemCallback<ServerCalendarEntity>() {
        override fun areItemsTheSame(oldItem: ServerCalendarEntity, newItem: ServerCalendarEntity): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: ServerCalendarEntity, newItem: ServerCalendarEntity): Boolean {
            return oldItem.id == newItem.id
                    && oldItem.getFirstName() == newItem.getFirstName()
                    && oldItem.getServerName() == newItem.getServerName()
                    && oldItem.getNote() == newItem.getNote()
                    && oldItem.getTime() == newItem.getTime()
                    && oldItem.remark == newItem.remark
                    && oldItem.type == newItem.type
                    && oldItem.notifySetting?.notifyTime == newItem.notifySetting?.notifyTime
                    && oldItem.notifySetting?.secondsAdvance == newItem.notifySetting?.secondsAdvance
                    && oldItem.notifySetting?.byApp == newItem.notifySetting?.byApp
                    && oldItem.notifySetting?.byWechat == newItem.notifySetting?.byWechat

        }
    }
) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ServersCalendarDetailViewHolder {
        return ServersCalendarDetailViewHolder(
            DialogServersCalendearDetailItemBinding.inflate(
                parent.layoutInflater,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: ServersCalendarDetailViewHolder, position: Int) {

        val data = getItem(position)

        holder.bindItem(data, parentViewModel.meEntity, parentViewModel.game)

        if (parentViewModel.meEntity?.isPartTime == true) {
            holder.binding.add.visibility = View.VISIBLE
        } else {
            holder.binding.add.visibility = View.GONE
        }
        holder.binding.add.setOnClickListener {
            fragment.startActivityForResult(
                AddKaiFuActivity.getIntent(
                    it.context,
                    data,
                    parentViewModel.serverCalendarLiveData.value as ArrayList<ServerCalendarEntity>,
                    parentViewModel.game.id,
                    parentViewModel.getSelectTime(
                        day = viewModel.day,
                        month = viewModel.month,
                        year = viewModel.year
                    )
                ), ServersCalendarActivity.GAME_DETAIL_ADD_KAIFU_REQUEST
            )
        }

        holder.itemView.layoutParams = (holder.itemView.layoutParams as ViewGroup.MarginLayoutParams).apply {
            bottomMargin = if (itemCount == position + 1) 7F.dip2px() else 0
        }

        holder.binding.remindTitle.visibility = View.GONE

        holder.binding.remind.visibility = if (viewModel.showRemind) View.VISIBLE else View.GONE

        holder.binding.remind.isSelected = data.notifySetting != null

        holder.binding.remind.setOnClickListener {
            CheckLoginUtils.checkLogin(it.context, "游戏详情-开服日历表-开服详情-开服提醒") {
                if ((System.currentTimeMillis() - (data.getTime() * 1000)) > 0) {
                    ToastUtils.showToast(it.context.getString(R.string.servers_calendar_time_out_of_date))
                    return@checkLogin
                }
                ServersCalendarDetailsRemindDialog().apply {
                    arguments = Bundle().apply {
                        putString(EntranceConsts.KEY_SERVER_CALENDAR_ID, data.id)
                    }
                    showNow(fragment.childFragmentManager, null)
                }
            }
        }
    }

    class ServersCalendarDetailViewHolder(val binding: DialogServersCalendearDetailItemBinding) :
        BaseRecyclerViewHolder<Any>(binding.root) {

        fun bindItem(data: ServerCalendarEntity, meEntity: MeEntity?, gameEntity: GameEntity) {
            binding.name.text = data.getNote()
            binding.time.text = data.getFormatTime("HH:mm")
            binding.remark.text = data.remark

            binding.name.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(binding.root.context))
            binding.time.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(binding.root.context))
            binding.remark.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(binding.root.context))

            if (meEntity?.isPartTime == true) {
                binding.name.paint.flags = Paint.UNDERLINE_TEXT_FLAG
                binding.name.paint.isAntiAlias = true
                binding.name.setOnClickListener {
                    val context = binding.root.context
                    if ("删档内测" == data.type || "不删档内测" == data.type || "公测" == data.type) {
                        Utils.toast(context, "开测信息不可编辑");
                    } else {
                        (context as Activity).startActivityForResult(
                            PatchKaifuActivity.getIntent(
                                context,
                                data,
                                gameEntity.id
                            ),
                            ServersCalendarActivity.GAME_DETAIL_PATCH_KAIFU_REQUEST
                        )
                    }
                }
            }
        }
    }
}