package com.sunny.app.soft.timberkeyboardnew.ui.activity

import android.content.Intent
import android.provider.Settings
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.sunny.app.soft.timberkeyboardnew.R
import com.sunny.app.soft.timberkeyboardnew.databinding.ActivityMainBinding
import com.sunny.app.soft.timberkeyboardnew.tools.DialogRecommend
import com.sunny.app.soft.timberkeyboardnew.tools.DialogStep
import com.sunny.app.soft.timberkeyboardnew.tools.KeyboardManager
import com.sunny.app.soft.timberkeyboardnew.topon.AdManager
import com.sunny.app.soft.timberkeyboardnew.ui.adapter.MainViewPagerAdapter
import com.sunny.app.soft.timberkeyboardnew.ui.fragment.CollectFragment
import com.sunny.app.soft.timberkeyboardnew.ui.fragment.HomeFragment
import com.sunny.app.soft.timberkeyboardnew.ui.fragment.SettingFragment

class MainActivity :
    BaseActivity(),
    View.OnClickListener,
    ViewPager.OnPageChangeListener {

    private lateinit var binding: ActivityMainBinding
    private lateinit var fragmentList: MutableList<Fragment>


    private var stepDialog: DialogStep? = null

    private var recommendDialog: DialogRecommend? = null
    override fun loadAd(): Boolean = true

    override fun setRootView(): View {
        binding = ActivityMainBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        super.initView()
        initViewPager()
        initTabButton()
        checkStep()
    }

    private fun initTabButton() {
        binding.mainTabHome.setOnClickListener(this)
        binding.mainTabSet.setOnClickListener(this)
        binding.mainTabCollect.setOnClickListener(this)
    }


    private fun checkStep() {
        if (!KeyboardManager.isEnable() || !KeyboardManager.isChoose()) {
            if (stepDialog == null) {
                stepDialog = DialogStep.newInstance(this@MainActivity) {
                    showRecommend()
                }
            }
            stepDialog?.show(supportFragmentManager, "")
        } else {
            showRecommend()
        }

    }

    private fun showRecommend() {
        if (recommendDialog == null) {
            recommendDialog = DialogRecommend.newInstance(this@MainActivity) {

            }
        }
        recommendDialog?.show(supportFragmentManager, "")
    }


    private fun initViewPager() {
        fragmentList = mutableListOf()
        fragmentList.add(HomeFragment())
        fragmentList.add(CollectFragment())
        fragmentList.add(SettingFragment())
        binding.mainViewpager.apply {
            adapter = MainViewPagerAdapter(fragmentList, supportFragmentManager)
            addOnPageChangeListener(this@MainActivity)
            offscreenPageLimit = 0
            setTabSelect(0)
        }
    }

    private fun setTabSelect(i: Int) {
        when (i) {
            0 -> {
                binding.mainTabHome.isSelected = true
                binding.mainTabCollect.isSelected = false
                binding.mainTabSet.isSelected = false
                binding.mainViewpager.currentItem = 0
            }

            1 -> {
                binding.mainTabHome.isSelected = false
                binding.mainTabCollect.isSelected = true
                binding.mainTabSet.isSelected = false
                binding.mainViewpager.currentItem = 1
            }

            2 -> {
                binding.mainTabHome.isSelected = false
                binding.mainTabCollect.isSelected = false
                binding.mainTabSet.isSelected = true
                binding.mainViewpager.currentItem = 2
            }
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            binding.mainTabHome -> {
                setTabSelect(0)
            }

            binding.mainTabCollect -> {
                setTabSelect(1)
            }

            binding.mainTabSet -> {
                setTabSelect(2)
            }
        }
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {
        setTabSelect(position)
    }

    override fun onPageScrollStateChanged(state: Int) {
    }

}