package com.audio.record.screen.test.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.audio.record.screen.test.R
import com.audio.record.screen.test.adapter.CropAdapter
import com.audio.record.screen.test.base.BaseFragment
import com.audio.record.screen.test.databinding.FragmentCropBinding
import com.audio.record.screen.test.viewmodel.PreviewViewModel

class CropFragment : BaseFragment<FragmentCropBinding>(), View.OnClickListener {


    private lateinit var navController: NavController
    private lateinit var viewModel: PreviewViewModel

    companion object {

        @JvmStatic
        fun newInstance() =
            CropFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }


    override fun initBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentCropBinding =
        FragmentCropBinding.inflate(inflater, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        navController = findNavController()

        viewModel = ViewModelProvider(requireActivity())[PreviewViewModel::class.java]
        binding.recycler.run {
            val stringList = resources.getStringArray(R.array.crop_text).toList()
            adapter = CropAdapter(requireContext()) {
                viewModel.updateCropText(it)

            }.apply { updateData(stringList) }
            layoutManager = LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
        }
        initClick()
    }

    private fun initClick() {
        binding.imClose.setOnClickListener(this)
        binding.imSave.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        v?.let {
            if (it == binding.imSave) {
                viewModel.updateClickCropSave(true)
            } else if (it == binding.imClose) {
                navController.navigateUp()
            }
        }
    }
}