package com.bearya.kids.grade

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import com.bearya.kids.BuildConfig
import com.bearya.kids.R
import com.bearya.kids.databinding.FragmentGradeBinding

class GradeFragment : Fragment(), View.OnClickListener, View.OnFocusChangeListener {

    private val gradeLevelPrimary = 1 // 小班
    private val gradeLevelMiddle = 2 // 中班
    private val gradeLevelLarge = 3 // 大班

    private lateinit var bindView: FragmentGradeBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        bindView = FragmentGradeBinding.inflate(inflater, container, false)
        bindView.lifecycleOwner = viewLifecycleOwner
        return bindView.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val versionName = "当前版本 : v ${BuildConfig.VERSION_NAME}.${BuildConfig.VERSION_CODE}"
        bindView.version.text = versionName
        bindView.gradePrimary.setOnClickListener(this)
        bindView.gradePrimary.onFocusChangeListener = this
        bindView.gradeMiddle.setOnClickListener(this)
        bindView.gradeMiddle.onFocusChangeListener = this
        bindView.gradeLarge.setOnClickListener(this)
        bindView.gradeLarge.onFocusChangeListener = this
        bindView.back.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.grade_primary -> v.findNavController().navigate(GradeFragmentDirections.actionGradeFragmentToChapterFragment(gradeLevelPrimary))
            R.id.grade_middle -> v.findNavController().navigate(GradeFragmentDirections.actionGradeFragmentToChapterFragment(gradeLevelMiddle))
            R.id.grade_large -> v.findNavController().navigate(GradeFragmentDirections.actionGradeFragmentToChapterFragment(gradeLevelLarge))
            R.id.back -> requireActivity().finish()
        }
    }

    override fun onFocusChange(v: View?, hasFocus: Boolean) {
        val scale = if (hasFocus) 1.2f else 1.0f
        v?.animate()?.scaleX(scale)?.scaleY(scale)?.start()
    }

}