package com.gamedog.vididin.main.fragments

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.ama.core.architecture.appBase.AppViewsFragment
import com.ama.core.architecture.appBase.OnFragmentBackgroundListener
import com.ama.core.architecture.util.setOnClickBatch
import com.ama.core.architecture.util.setStatusBarDarkFont
import com.gamedog.vididin.R
import com.gamedog.vididin.main.WatchVideoDialog
import com.gamedog.vididin.main.fragments.task.DailySignDialog
import com.gamedog.vididin.main.fragments.task.TaskBean
import com.gamedog.vididin.main.interfaces.OnTabClickAgainListener
import com.gamedog.vididin.manager.TaskManager
import com.gamedog.vididin.router.Router
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import kotlin.getValue
import com.gamedog.vididin.databinding.VididinappFeatureMessageFragmentMessageBinding as ViewBinding
import com.gamedog.vididin.main.fragments.home.YoutubeViewModel as ViewModel
import com.gamedog.vididin.main.fragments.home.YoutubeUiState as UiState



@AndroidEntryPoint
class TasksFragment : AppViewsFragment<ViewBinding, UiState, ViewModel>(), OnTabClickAgainListener, OnFragmentBackgroundListener {

    private var mTaskConfig: TaskBean? = null
    override val mViewModel: ViewModel by viewModels()
    override var isBackgroundBright: Boolean = true


    override fun inflateViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ) = ViewBinding.inflate(inflater, container, false)

    override fun ViewBinding.initWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(root) { v, insets ->
            val systemBars =
                insets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
            v.updatePadding(top = systemBars.top)
            insets
        }
    }


    override fun ViewBinding.initViews() {
        with(binding) {
            setOnClickBatch(ivGotoDailySign, llTaskBenefit, llTaskWatchVideo, llTaskGame, llTaskZerobuy, tvWatchVideo, tvWithdraw) {
                when(this) {
                    ivGotoDailySign->{
                        DailySignDialog(context).build().show()
                    }

                    llTaskBenefit->{
                        Router.Benefit.startActivity(requireActivity())
                    }

                    llTaskWatchVideo->{
                        WatchVideoDialog(requireContext()).show()
                    }

                    llTaskGame->{

                    }

                    llTaskZerobuy->{
                        Router.ZeroBuy.startActivity(requireActivity())
                    }

                    tvWatchVideo->{
                        WatchVideoDialog(requireContext()).show()
                    }

                    tvWithdraw->{
                        Router.Withdraw.startActivity(requireActivity())
                    }
                }
            }



            beginnerTaskWithdraw.initUI(R.mipmap.icon_cash_s, R.string.beginner_withdraw_title, R.string.beginner_withdraw_desc,R.string.resgatar)
            beginnerTaskWithdraw.setRewardIcon(R.mipmap.icon_cash_s)
            beginnerTaskWithdraw.setRewardTextStyle(21, R.color.green_39)
            beginnerTaskWithdraw.setActionFun {
                gotoWithDraw()
            }

            beginnerTaskDiscord.initUI(R.mipmap.icon_dicord, R.string.beginner_discord_title, R.string.beginner_discord_desc,R.string.resgatar)
            beginnerTaskDiscord.setActionFun {
                gotoDiscord()
            }

            beginnerTaskEnableNotify.initUI(R.mipmap.icon_notify, R.string.beginner_notify_title, R.string.beginner_notify_desc,R.string.Resgatado)
            beginnerTaskEnableNotify.setActionFun {
                gotoNotification()
            }

            dailyTaskWatch1.initUI(R.mipmap.icon_video_task, R.string.daily_video_task_title, 1, R.string.resgatar)
            dailyTaskWatch1.setActionFun {
                gotoWithVideo()
            }

            dailyTaskWatch5.initUI(R.mipmap.icon_video_task, R.string.daily_video_task_title, 5, R.string.go_and_do)
            dailyTaskWatch5.setActionFun {
                gotoWithVideo()
            }

            dailyTaskWatch10.initUI(R.mipmap.icon_video_task, R.string.daily_video_task_title, 10, R.string.go_and_do)
            dailyTaskWatch10.setActionFun {
                gotoWithVideo()
            }

            dailyTaskWatch20.initUI(R.mipmap.icon_video_task, R.string.daily_video_task_title, 20, R.string.go_and_do)
            dailyTaskWatch20.setActionFun {
                gotoWithVideo()
            }

            dailyTaskWatch30.initUI(R.mipmap.icon_video_task, R.string.daily_video_task_title, 30, R.string.go_and_do)
            dailyTaskWatch30.setActionFun {
                gotoWithVideo()
            }
        }

        lifecycleScope.launch {
            mTaskConfig = TaskManager.instance().getTaskConfig()
        }
    }

    override fun ViewBinding.initListeners() {
    }

    override fun ViewBinding.initObservers() {
    }

    override fun ViewBinding.onUiStateCollect(uiState: UiState) {

    }

    override fun onResume() {
        super.onResume()
        setStatusBarDarkFont(isDarkFont = isBackgroundBright)
    }

    override fun onTabClickAgain() {
    }

    companion object {
        internal fun newInstance() = TasksFragment()
    }

    private fun gotoWithDraw() {
        Router.Withdraw.startActivity(requireActivity())
    }

    private fun gotoDiscord() {
        //TODO("Not yet implemented")
    }

    private fun gotoNotification() {
        //TODO("Not yet implemented")
    }

    private fun gotoWithVideo() {
        //TODO("Not yet implemented")
    }


}