package com.gh.gamecenter.cloudarchive

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.gh.gamecenter.cloudarchive.ArchiveLimitAdapter.ArchiveItem
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.databinding.FragmentArchiveLimitDialogBinding
import com.gh.gamecenter.entity.ArchiveEntity
import com.halo.assistant.HaloApp

class ArchiveLimitDialogFragment : BaseDialogFragment() {

    private lateinit var binding: FragmentArchiveLimitDialogBinding

    private var dataList: List<ArchiveEntity> = emptyList()
    private var listener: ArchiveLimitSelectedListener? = null

    private val adapter by lazy {
        ArchiveLimitAdapter(requireContext())
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is ArchiveLimitSelectedListener) {
            listener = context
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataList = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            arguments?.getParcelableArrayList(KEY_ARCHIVE_DATA, ArchiveEntity::class.java)
        } else {
            arguments?.getParcelableArrayList(KEY_ARCHIVE_DATA)
        } ?: emptyList()
    }

    override fun onStart() {
        super.onStart()
        val width = HaloApp.getInstance().application.resources.displayMetrics.widthPixels - DisplayUtils.dip2px(60F)
        val height = dialog?.window?.attributes?.height ?: ViewGroup.LayoutParams.WRAP_CONTENT
        dialog?.window?.setLayout(width, height)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return FragmentArchiveLimitDialogBinding.inflate(inflater, container, false)
            .also {
                binding = it
            }.root
    }

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

        binding.rvArchives.layoutManager = LinearLayoutManager(context)
        binding.rvArchives.adapter = adapter

        adapter.setListData(dataList.map(::ArchiveItem).toMutableList())

        binding.tvCancel.setOnClickListener {
            dismiss()
        }

        binding.tvConfirm.setOnClickListener {
            dismiss()
            listener?.onSelected(adapter.getSelectedArchive())
        }
    }

    companion object {

        private const val KEY_ARCHIVE_DATA = "key_archive_data"

        private const val ARCHIVE_TAG = "archive_tah"

        fun show(
            fragmentManager: FragmentManager,
            archiveList: ArrayList<ArchiveEntity>
        ) {
            val fragment = ArchiveLimitDialogFragment()
            fragment.arguments = Bundle().apply {
                putParcelableArrayList(KEY_ARCHIVE_DATA, archiveList)
            }
            fragment.show(fragmentManager, ARCHIVE_TAG)
        }
    }
}