package com.ytpm.hydtw.activity

import android.os.Bundle
import android.widget.RadioGroup
import android.widget.RadioGroup.OnCheckedChangeListener
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.blankj.utilcode.util.ToastUtils
import com.ytpm.hydtw.R
import com.ytpm.hydtw.adapter.NotePagerAdapter
import com.ytpm.hydtw.databinding.ActivityMainBinding
import com.ytpm.hydtw.utils.DateTimeUtils

class MainActivity : FragmentActivity(), OnPageChangeListener, OnCheckedChangeListener {
    private var clickTime: Int = 0
    private var millisTimestamp1: Long = 0L
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)

        setContentView(binding.root)
        initView()
    }

    private fun initView() {

        binding.rb1.isChecked = true

        val pagerAdapter = NotePagerAdapter(supportFragmentManager)
        binding.vp.adapter = pagerAdapter

//        binding.vp.addOnPageChangeListener(this)

//        binding.rg.setOnCheckedChangeListener(this)
    }

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

    override fun onPageSelected(position: Int) {
        when (position) {
            0 -> binding.rb1.isChecked = true
            1 -> binding.rb2.isChecked = true
            2 -> binding.rb3.isChecked = true
        }
    }

    override fun onPageScrollStateChanged(state: Int) {
    }

    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
//        var currentPosition = 0
//        when (checkedId) {
//            R.id.rb1 -> currentPosition = 0
//            R.id.rb2 -> currentPosition = 1
//            R.id.rb3 -> currentPosition = 2
//        }

//        binding.vp.currentItem = currentPosition

    }

    @Deprecated("Deprecated in Java")
    override fun onBackPressed() {

        if (clickTime == 0) {
            millisTimestamp1 = System.currentTimeMillis()
            ToastUtils.showLong("再次点击退出应用！")
            clickTime++
            return
        }

        if (System.currentTimeMillis() - millisTimestamp1 < 1000) {
            super.onBackPressed()
        } else {
            clickTime = 0
        }
    }
}
