package com.all.pdfreader.pro.app.ui.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.all.pdfreader.pro.app.R
import com.all.pdfreader.pro.app.databinding.DialogSortBinding
import com.all.pdfreader.pro.app.model.SortConfig
import com.all.pdfreader.pro.app.model.SortDirection
import com.all.pdfreader.pro.app.model.SortField
import com.all.pdfreader.pro.app.sp.AppStore
import com.all.pdfreader.pro.app.ui.fragment.FavoriteFrag
import com.all.pdfreader.pro.app.ui.fragment.HomeFrag
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class SortDialogFragment(
    private val fragment: Fragment,
    private val onOkClick: (String) -> Unit
) : BottomSheetDialogFragment() {

    private lateinit var binding: DialogSortBinding
    private lateinit var sortConfig: SortConfig
    private lateinit var selectedField: SortField
    private lateinit var selectedDirection: SortDirection

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        binding = DialogSortBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
            ?.setBackgroundResource(R.drawable.dr_rc_top_12_bg_white)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val appStore = AppStore(requireActivity())
        isCancelable = false
        when(fragment){
            is HomeFrag->{
                sortConfig = SortConfig.fromPreferenceString(appStore.documentSortType)
            }
            is FavoriteFrag->{
                sortConfig = SortConfig.fromPreferenceString(appStore.favoriteSortType)
            }
        }
        updateUi(sortConfig)
        selectedField = sortConfig.field
        selectedDirection = sortConfig.direction

        binding.createdDateBtn.setOnClickListener {
            selectedField = SortField.DATE
            updateUi(SortConfig(selectedField,selectedDirection))
        }

        binding.fileNameBtn.setOnClickListener {
            selectedField = SortField.NAME
            updateUi(SortConfig(selectedField,selectedDirection))
        }

        binding.fileSizeBtn.setOnClickListener {
            selectedField = SortField.SIZE
            updateUi(SortConfig(selectedField,selectedDirection))
        }

        binding.ascendingBtn.setOnClickListener {
            selectedDirection = SortDirection.ASC
            updateUi(SortConfig(selectedField,selectedDirection))
        }

        binding.descendingBtn.setOnClickListener {
            selectedDirection = SortDirection.DESC
            updateUi(SortConfig(selectedField,selectedDirection))
        }

        binding.cancelBtn.setOnClickListener {
            dismiss()
        }
        binding.okBtn.setOnClickListener {
            val newConfig = SortConfig(
                field = selectedField, direction = selectedDirection
            )
            onOkClick(newConfig.toPreferenceString())
            dismiss()
        }
    }

    private fun updateUi(config: SortConfig){
        when (config.field) {
            SortField.DATE -> {
                binding.createdDateIv.setImageResource(R.drawable.icon_created_date)
                binding.createdDateTv.setTextColor(requireActivity().getColor(R.color.icon_on))

                binding.fileNameIv.setImageResource(R.drawable.icon_sort_name_off)
                binding.fileNameTv.setTextColor(requireActivity().getColor(R.color.icon_off))

                binding.fileSizeIv.setImageResource(R.drawable.icon_sort_size_off)
                binding.fileSizeTv.setTextColor(requireActivity().getColor(R.color.icon_off))

                binding.createdDateGou.visibility = View.VISIBLE
                binding.fileNameGou.visibility = View.GONE
                binding.fileSizeGou.visibility = View.GONE
            }

            SortField.NAME -> {
                binding.createdDateIv.setImageResource(R.drawable.icon_created_date_off)
                binding.createdDateTv.setTextColor(requireActivity().getColor(R.color.icon_off))

                binding.fileNameIv.setImageResource(R.drawable.icon_sort_name)
                binding.fileNameTv.setTextColor(requireActivity().getColor(R.color.icon_on))

                binding.fileSizeIv.setImageResource(R.drawable.icon_sort_size_off)
                binding.fileSizeTv.setTextColor(requireActivity().getColor(R.color.icon_off))

                binding.createdDateGou.visibility = View.GONE
                binding.fileNameGou.visibility = View.VISIBLE
                binding.fileSizeGou.visibility = View.GONE
            }

            SortField.SIZE -> {
                binding.createdDateIv.setImageResource(R.drawable.icon_created_date_off)
                binding.createdDateTv.setTextColor(requireActivity().getColor(R.color.icon_off))

                binding.fileNameIv.setImageResource(R.drawable.icon_sort_name_off)
                binding.fileNameTv.setTextColor(requireActivity().getColor(R.color.icon_off))

                binding.fileSizeIv.setImageResource(R.drawable.icon_sort_size)
                binding.fileSizeTv.setTextColor(requireActivity().getColor(R.color.icon_on))

                binding.createdDateGou.visibility = View.GONE
                binding.fileNameGou.visibility = View.GONE
                binding.fileSizeGou.visibility = View.VISIBLE
            }
        }
        when (config.direction) {
            SortDirection.ASC -> {
                binding.ascendingIv.setImageResource(R.drawable.icon_ascending)
                binding.ascendingTv.setTextColor(requireActivity().getColor(R.color.icon_on))

                binding.descendingIv.setImageResource(R.drawable.icon_descending_off)
                binding.descendingTv.setTextColor(requireActivity().getColor(R.color.icon_off))

                binding.ascendingGou.visibility = View.VISIBLE
                binding.descendingGou.visibility = View.GONE
            }

            SortDirection.DESC -> {
                binding.ascendingIv.setImageResource(R.drawable.icon_ascending_off)
                binding.ascendingTv.setTextColor(requireActivity().getColor(R.color.icon_off))

                binding.descendingIv.setImageResource(R.drawable.icon_descending)
                binding.descendingTv.setTextColor(requireActivity().getColor(R.color.icon_on))

                binding.ascendingGou.visibility = View.GONE
                binding.descendingGou.visibility = View.VISIBLE
            }
        }
    }
}