package ru.myitschool.work.ui.profile

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.squareup.picasso.Picasso
import dagger.hilt.android.AndroidEntryPoint
import ru.myitschool.work.R
import ru.myitschool.work.databinding.FragmentProfileBinding
import ru.myitschool.work.ui.login.LoginDestination
import ru.myitschool.work.ui.qr.result.QrResultDestination
import ru.myitschool.work.ui.qr.scan.QrScanDestination
import ru.myitschool.work.utils.collectWhenStarted
import ru.myitschool.work.utils.visibleOrGone

@AndroidEntryPoint
class ProfileFragment : Fragment(R.layout.fragment_profile) {
    private var _binding: FragmentProfileBinding? = null
    private val binding: FragmentProfileBinding get() = _binding!!

    private val viewModel: ProfileViewModel by viewModels()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        _binding = FragmentProfileBinding.bind(view)
        initCallback()
        subscribe()
    }

    private fun subscribe() {
        viewModel.state.collectWhenStarted(this) { state ->
            binding.showState.visibleOrGone(state is ProfileViewModel.State.Show)
            binding.error.visibleOrGone(state is ProfileViewModel.State.Error)
            binding.loading.visibleOrGone(state is ProfileViewModel.State.Loading)

            when(state) {
                is ProfileViewModel.State.Loading -> Unit
                is ProfileViewModel.State.Error -> {
                    binding.error.text = state.errorText
                }
                is ProfileViewModel.State.Show -> {
                    binding.fullname.text = state.fullname
                    binding.position.text = state.position
                    binding.lastEntry.text = state.lastEntry
                    Picasso.get()
                        .load(state.imageUrl)
                        .error(R.drawable.ic_no_img)
                        .into(binding.photo)
                }
            }
        }

        viewModel.action.collectWhenStarted(this) { action ->
            when(action) {
                is ProfileViewModel.Action.OpenLogin -> {
                    findNavController().navigate(LoginDestination) {
                        popUpTo<ProfileDestination> { inclusive = true }
                    }
                }

                is ProfileViewModel.Action.OpenScan -> {
                    findNavController().apply {
                        navigate(QrResultDestination)
                        navigate(QrScanDestination)
                    }
                }
            }
        }
    }

    private fun initCallback() {
        binding.refresh.setOnClickListener { viewModel.clickRefresh() }
        binding.logout.setOnClickListener { viewModel.clickLogout() }
        binding.scan.setOnClickListener { viewModel.clickScan() }
    }

    override fun onDestroyView() {
        _binding = null
        super.onDestroyView()
    }
}