package com.abbidot.tracker.ui.activity.set

import android.widget.CompoundButton
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.abbidot.tracker.R
import com.abbidot.tracker.adapter.TextViewAdapter
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.DataBean
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityNotificationsSetBinding
import com.abbidot.tracker.vm.AppSetViewModel


class NotificationsSetActivity :
    BaseActivity<ActivityNotificationsSetBinding>(ActivityNotificationsSetBinding::inflate),
    CompoundButton.OnCheckedChangeListener {

    private val mAppSetViewModel: AppSetViewModel by viewModels()

    override fun getTopBar() = mViewBinding.ilNotificationsSetTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle(R.string.txt_notifications_set)
        setLeftBackImage(R.drawable.icon_white_back_svg)

        mViewBinding.apply {

            ilNotificationsSetDisplayFences.cbNotificationsSetItemSwitch.setOnCheckedChangeListener(
                this@NotificationsSetActivity
            )

            ilNotificationsSetTrackerStatus.let {
                it.tvNotificationsSetItemTitle.setText(R.string.txt_tracker_status)
                it.tvNotificationsSetItemDescription.setText(R.string.txt_tracker_status_desc)
                it.cbNotificationsSetItemSwitch.setOnCheckedChangeListener(this@NotificationsSetActivity)
            }

            ilNotificationsSetTrackerActivity.let {
                it.tvNotificationsSetItemTitle.setText(R.string.txt_activity)
                it.tvNotificationsSetItemDescription.setText(R.string.txt_activity_desc)
                it.cbNotificationsSetItemSwitch.setOnCheckedChangeListener(this@NotificationsSetActivity)
            }

            val list = mutableListOf<DataBean>()
            for (i in 0 until 10) {
                DataBean().apply {
                    name = "aaaa$i"
                    list.add(this)
                }
            }
            val layoutManager = LinearLayoutManager(mContext)
            layoutManager.orientation = LinearLayoutManager.VERTICAL
            layoutManager.stackFromEnd = true
            layoutManager.reverseLayout = true
            layoutManager.isItemPrefetchEnabled = false
            rvList.layoutManager = layoutManager
            rvList.setHasFixedSize(true)


            rvList.addOnScrollListener(object : RecyclerView.OnScrollListener() {

                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)
                    layoutManager.isItemPrefetchEnabled = newState == RecyclerView.SCROLL_STATE_IDLE
                }
            })
            val textViewAdapter = TextViewAdapter(mContext, list)
            rvList.adapter = textViewAdapter
        }

        mAppSetViewModel.getNotificationSet()
    }

    override fun liveDataObserve() {
        mAppSetViewModel.apply {
            mGetNotificationLiveData.observe(this@NotificationsSetActivity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        it.getOrNull()?.let { data ->
                            for (d in data) {
                                when (d.notificationType) {
                                    1 -> mViewBinding.ilNotificationsSetDisplayFences.cbNotificationsSetItemSwitch.isChecked =
                                        d.status == 1

                                    2 -> mViewBinding.ilNotificationsSetTrackerStatus.cbNotificationsSetItemSwitch.isChecked =
                                        d.status == 1

                                    3 -> mViewBinding.ilNotificationsSetTrackerActivity.cbNotificationsSetItemSwitch.isChecked =
                                        d.status == 1
                                }
                            }
                        }
                    }
                })
            }

            mSetNotificationLiveData.observe(this@NotificationsSetActivity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        showToast(R.string.txt_change_successful)
                    }
                })
            }
        }
    }

    private fun setNotificationType(notificationType: Int, isChecked: Boolean) {
        val status = if (isChecked) 1 else 0
        mAppSetViewModel.setNotification(notificationType, status)
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        if (!buttonView!!.isPressed) return
        mViewBinding.apply {
            when (buttonView) {
                ilNotificationsSetDisplayFences.cbNotificationsSetItemSwitch -> setNotificationType(
                    1, isChecked
                )

                ilNotificationsSetTrackerStatus.cbNotificationsSetItemSwitch -> setNotificationType(
                    2, isChecked
                )

                ilNotificationsSetTrackerActivity.cbNotificationsSetItemSwitch -> setNotificationType(
                    3, isChecked
                )
            }
        }
    }
}