package com.webtoonviewer.ui

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.webtoonviewer.R
import com.webtoonviewer.ui.adapter.TagAdapter
import com.webtoonviewer.ui.data.CheckboxItem
import com.webtoonviewer.utils.SharedPreferencesHelper

class DialogMainSettingsFragment: DialogFragment() {
    private lateinit var tagsVisibilityPreferences: SharedPreferencesHelper

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.dialog_main_settings, container, false)
    }

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

        tagsVisibilityPreferences = SharedPreferencesHelper(requireContext(), "tags_visibility")

        val checkBoxShowAll: CheckBox = view.findViewById(R.id.checkBoxDialogMainSettingsShowAll)
        checkBoxShowAll.isChecked = tagsVisibilityPreferences.getBoolean("showAll", false)

        // Initialize RecyclerView
        val recyclerView: RecyclerView = view.findViewById(R.id.recyclerViewDialogMainSettingsTagList)
        recyclerView.layoutManager = LinearLayoutManager(requireContext())
        val tagsItems = loadCheckboxItems()
        val tagAdapter = TagAdapter(tagsItems) { position, isChecked ->
            tagsItems[position].isChecked = isChecked
            if (!isChecked) {
                checkBoxShowAll.isChecked = false
            }
        }

        recyclerView.adapter = tagAdapter

        checkBoxShowAll.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                tagAdapter.setItemsChecked()
            }
        }

        val subMitButton: Button = view.findViewById(R.id.btnDialogMainSettingsSubmit)
        subMitButton.setOnClickListener {
            var refresh_necessary = false
            if (tagsVisibilityPreferences.getBoolean("showAll", false) != checkBoxShowAll.isChecked) {
                tagsVisibilityPreferences.putBoolean("showAll", checkBoxShowAll.isChecked)
                refresh_necessary = true
            }

            tagsItems.forEach { item ->
                if (tagsVisibilityPreferences.getBoolean(item.text, false) != item.isChecked) {
                    tagsVisibilityPreferences.putBoolean(item.text, item.isChecked)
                    refresh_necessary = true
                }
            }
            dismiss()
            if (refresh_necessary) {
                (activity as? MainActivity)?.refreshActivity() // Refresh the activity
            }
        }

        val dirButton: Button = view.findViewById(R.id.btnDialogMainSettingsDirPage)
        dirButton.setOnClickListener {
            dismiss()
            val intent = Intent(requireContext(), DirectoryActivity::class.java)
            startActivity(intent)
        }
    }

    private fun loadCheckboxItems(): MutableList<CheckboxItem> {
        val tags = resources.getStringArray(R.array.tag_items)
        val items = mutableListOf<CheckboxItem>()
        for (tag in tags) {
            val isChecked = tagsVisibilityPreferences.getBoolean(tag, false)
            items.add(CheckboxItem(tag, isChecked))
        }
        return items
    }
}