package com.ux.video.file.filerecovery.photo

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.drawable.toDrawable
import androidx.fragment.app.DialogFragment
import com.ux.video.file.filerecovery.R
import com.ux.video.file.filerecovery.databinding.CommonLayoutSortItemBinding
import com.ux.video.file.filerecovery.databinding.DialogSortBinding


class SortDialogFragment(val onClickSort: (type: Int) -> Unit) : DialogFragment() {

    private lateinit var binding: DialogSortBinding
    private var clickType = PhotoSortingActivity.SORT_DESC_DATE

    private lateinit var LayoutList: List<CommonLayoutSortItemBinding>
    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            setGravity(Gravity.BOTTOM)
            setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DialogSortBinding.inflate(inflater)
        binding.run {
            LayoutList = listOf(
                ascendingSizeLayout,
                descendingSizeLayout,
                ascendingDateLayout,
                descendingDateLayout
            )
            setMutualExclusion(LayoutList)
            resetSelectedSortType(clickType)
            setIconText(
                LayoutList[0],
                R.drawable.icon_ascending_size,
                getString(R.string.size),
                getString(R.string.asc_size_describe)
            )
            setIconText(
                LayoutList[1],
                R.drawable.icon_descending_size,
                getString(R.string.size),
                getString(R.string.desc_size_describe)
            )
            setIconText(
                LayoutList[2],
                R.drawable.icon_ascending_date,
                getString(R.string.date),
                getString(R.string.asc_date_describe)
            )
            setIconText(
                LayoutList[3],
                R.drawable.icon_descending_date,
                getString(R.string.date),
                getString(R.string.desc_date_describe)
            )

            tvCancel.setOnClickListener { dismiss() }
            tvOk.setOnClickListener {
                onClickSort(clickType)
                dismiss()
            }
        }
        return binding.root
    }


    fun resetSelectedSortType(type: Int) {
        LayoutList.forEachIndexed { index, item ->
            item.checkImage.isSelected = index == type
        }
    }


    private fun setIconText(
        itemBinding: CommonLayoutSortItemBinding,
        iconId: Int,
        text: String,
        subText: String
    ) {
        itemBinding.run {
            sortType.text = text
            sortSubType.text = subText
            icon.setImageResource(iconId)
        }
    }

    private fun setMutualExclusion(listOf: List<CommonLayoutSortItemBinding>) {
        for (i in 0 until listOf.size) {
            val binding = listOf[i]
            binding.root.setOnClickListener {
                for (j in 0 until listOf.size) {
                    listOf[j].checkImage.isSelected = false
                }
                clickType = i
                binding.checkImage.isSelected = true
            }
        }

    }
}
