/*
 * Lunar Launcher
 * Copyright (C) 2022 Md Rasel Hossain
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package rasel.lunar.launcher.home.adapters

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import rasel.lunar.launcher.LauncherActivity.Companion.lActivity
import rasel.lunar.launcher.R
import rasel.lunar.launcher.databinding.ListItemBinding
import rasel.lunar.launcher.model.jGuruTag

internal class RssTagAdapter(
    private val smsList: ArrayList<jGuruTag>,
    private val context: Context) : RecyclerView.Adapter<RssTagAdapter.RssTag>() {

    private val currentFragment = lActivity!!.supportFragmentManager.findFragmentById(R.id.mainFragmentsContainer)

    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): RssTag {
        val binding = ListItemBinding.inflate(LayoutInflater.from(viewGroup.context), viewGroup, false)
        return RssTag(binding)
    }

    override fun getItemCount(): Int {
        return smsList.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: RssTag, position: Int) {
        val todo = smsList[position]

        holder.view.itemText.text =  "\u25CF  ${Gson().toJson(todo)}"
        /* multiline texts are enabled for TodoManager */
//        holder.view.itemText.isSingleLine = false
        /* launch edit or update dialog on item click */
//        holder.view.itemText.setOnClickListener { updateDialog(position) }
        /* copy texts on long click */
        holder.view.itemText.setOnLongClickListener {

            true
        }

    }

    inner class RssTag(var view: ListItemBinding) : RecyclerView.ViewHolder(view.root)

    fun updateData(newList: List<jGuruTag>) {
        val diffUtilResult = DiffUtil.calculateDiff(RssTagDiffUtil(smsList, newList))
        diffUtilResult.dispatchUpdatesTo(this)
//        smsList.clear()
//        smsList.addAll(newList)
    }

    /* update dialog */
    private fun updateDialog(position: Int) {
//        val bottomSheetDialog = BottomSheetDialog(lActivity!!, R.style.BottomSheetDialog)
//        val dialogBinding = TodoDialogBinding.inflate(LayoutInflater.from(context))
//        bottomSheetDialog.setContentView(dialogBinding.root)
//        bottomSheetDialog.show()
//        bottomSheetDialog.dismissWithAnimation = true
//
//        val databaseHandler = DatabaseHandler(context)
//        val todo = databaseHandler.todos[position]
//
//        dialogBinding.apply {
//            deleteAllConfirmation.visibility = View.GONE
//            todoInput.setText(todo.name)
//            todoCancel.text = context.getString(R.string.delete)
//            todoCancel.setTextColor(ContextCompat.getColor(context, android.R.color.holo_red_light))
//            todoOk.text = context.getString(R.string.update)
//        }
//
//        /* delete the item */
//        dialogBinding.todoCancel.setOnClickListener {
//
//        }
//
//        /* update the item */
//        dialogBinding.todoOk.setOnClickListener {
//            val updatedTodoString = Objects.requireNonNull(dialogBinding.todoInput.text).toString().trim { it <= ' ' }
//            if (updatedTodoString.isNotEmpty()) {
//                todo.name = updatedTodoString
//                databaseHandler.updateTodo(todo)
//                bottomSheetDialog.dismiss()
//            } else {
//                dialogBinding.todoInput.error = context.getString(R.string.empty_text_field)
//            }
//        }
    }

}

internal class RssTagDiffUtil(
    private val oldList: List<jGuruTag>, private val newList: List<jGuruTag>
) : DiffUtil.Callback() {

    override fun getOldListSize(): Int = oldList.size
    override fun getNewListSize(): Int = newList.size

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
        oldList[oldItemPosition].link == newList[newItemPosition].link

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
        oldList[oldItemPosition].link == newList[newItemPosition].link
}
