/*
 * 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.apps

import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.util.TypedValue
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.updatePadding
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.textview.MaterialTextView
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async
import rasel.lunar.launcher.LauncherActivity.Companion.lActivity
import rasel.lunar.launcher.R
import rasel.lunar.launcher.apps.IconPackManager.Companion.getDrawableIconForPackage
import rasel.lunar.launcher.databinding.AppsChildBinding
import rasel.lunar.launcher.databinding.ContactItemBinding
import rasel.lunar.launcher.helpers.UniUtils.Companion.dpToPx
import rasel.lunar.launcher.utils.BLog


internal class ContactAdapter (
    private val packageManager: PackageManager,
    private val fragmentManager: FragmentManager) : RecyclerView.Adapter<ContactAdapter.ContactViewHolder>() {

    private var oldList = mutableListOf<SimpleContact>()
    private var appGravity: Int = Gravity.CENTER

    companion object {
        @JvmStatic var appsSize: Int? = null
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ContactViewHolder =
        ContactViewHolder(ContactItemBinding.inflate(LayoutInflater.from(viewGroup.context), viewGroup, false))

    override fun onBindViewHolder(holder: ContactViewHolder, i: Int) {
        val item = oldList[i]


        holder.view.apply {
            name.text = item.name
            number.text= item.phoneNumber
        }

        holder.view.root.apply {
            /* on click - open app */
            setOnClickListener {
                ContactMenu().show(fragmentManager, item.id.toString())
            }

            /* on long click - open app menu */
            setOnLongClickListener {
                BLog.LOGE("item.id.toString() >> ${item.id.toString()}")
                ContactMenu().show(fragmentManager, item.id.toString())
                true
            }
        }
    }

    override fun getItemCount(): Int = oldList.size

    inner class ContactViewHolder(var view: ContactItemBinding) : RecyclerView.ViewHolder(view.root)

    /* update app list */
    fun updateData(newList: List<SimpleContact>) {
        val diffUtilResult = DiffUtil.calculateDiff(ContactDiffUtil(oldList, newList))
        diffUtilResult.dispatchUpdatesTo(this)
        oldList.clear()
        oldList.addAll(newList)
        newList.size.let {
            appsSize = it
        }
    }

    /* update text gravity (alignment) */
    @SuppressLint("RtlHardcoded", "NotifyDataSetChanged")
    fun updateGravity(gravity: Int){
        /* the first check is to avoid calling notifyDataSetChanged() everytime */
        if (gravity != appGravity &&
            (gravity == Gravity.LEFT || gravity == Gravity.CENTER || gravity == Gravity.RIGHT)) {
            appGravity = gravity
            notifyDataSetChanged()
        }
    }

    fun hideItem(idx: Int) {

    }
}
data class SimpleContact(val id : String,val name : String ,val phoneNumber : String)

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

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

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

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