package com.gamedog.vididin.features.winrecords

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.ama.core.architecture.appBase.AppViewsEmptyViewModelActivity
import com.ama.core.architecture.util.CommonItemDecoration
import com.gamedog.vididin.VididinEvents
import com.gamedog.vididin.beans.ZeroBuyResp
import com.gamedog.vididin.features.zero.ZeroBuyViewModel
import com.gamedog.vididin.features.zero.ZeroRecordAdapter
import com.viddin.videos.free.R
import com.gamedog.vididin.main.interfaces.OnTabStyleListener
import com.gamedog.vididin.manager.ZeroManager
import com.gamedog.vididin.netbase.Result
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import kotlin.getValue
import com.viddin.videos.free.databinding.ActivityWinRecordBinding as ViewBinding



@AndroidEntryPoint
class WinRecordsActivity : AppViewsEmptyViewModelActivity<ViewBinding>(), OnTabStyleListener {

    private val viewModel: ZeroBuyViewModel by viewModels()
    private lateinit var mAdapter: ZeroRecordAdapter


    override fun inflateViewBinding(inflater: LayoutInflater) = ViewBinding.inflate(inflater)
    override fun ViewBinding.initViews() {

        with(binding) {
            titlebar.setBackIconColor(R.color.black)
            titlebar.setTitleText(R.string.lottery_record, R.color.black)



            with(recyclerView) {
                mAdapter = ZeroRecordAdapter({
                    ZeroManager.instance().startWithdrawProcess(this@WinRecordsActivity, it)
                })
                adapter = mAdapter
                layoutManager = LinearLayoutManager(this@WinRecordsActivity, LinearLayoutManager.VERTICAL, false)
                addItemDecoration(
                    CommonItemDecoration.create(horizontalSpace = 0, verticalSpace = 30)
                )
            }
        }

    }

    override fun ViewBinding.initWindowInsets() {
        setImmerseRootView(contentRoot)
    }

    override fun ViewBinding.initListeners() {

        registerEvents({ data->
            when (data?.mEventType) {
                VididinEvents.EVENT_ZERO_WITHDRAW_LIST_CHANGED -> {
                    mAdapter.notifyDataSetChanged()
                }
            }

        }, VididinEvents.EVENT_ZERO_WITHDRAW_LIST_CHANGED,)



        requestData()
    }

    override fun ViewBinding.initObservers() {
        //TODO("Not yet implemented")
    }

    override fun onTabIsDarkFont(isDarkFont: Boolean) {
        //TODO("Not yet implemented")
    }

    private fun requestData() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.ZeroBuyListData.collect { result ->
                    when (result) {
                        is Result.Loading -> { showLoading(false) }
                        is Result.Success -> {
                            hideLoading()
                            updateUIs(result.data)
                        }
                        is Result.Error -> {
                            hideLoading()
                        }
                    }
                }
            }
        }

        viewModel.requestZeroBuyInfo()
    }

    private fun updateUIs(data: ZeroBuyResp) {
        if (data.finished_purchases.isNullOrEmpty()) {
            binding.tvNoDataHint.isVisible = true
            binding.recyclerView.isVisible = false
        } else {
            binding.tvNoDataHint.isVisible = false
            binding.recyclerView.isVisible = true
            mAdapter.submitList(data.finished_purchases)
        }
    }

    companion object {
        internal fun startActivity(activity: Activity) {
            activity.startActivity(Intent(activity.applicationContext, WinRecordsActivity::class.java))
        }
    }

}