package com.sambath.kunkhmer.adapter

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sambath.kunkhmer.R
import com.sambath.kunkhmer.adapter.FighterAdapter.ViewHolder
import com.sambath.kunkhmer.config.Config
import com.sambath.kunkhmer.remote.Fighters
import com.sambath.kunkhmer.remote.Matches
import com.sambath.kunkhmer.remote.service.ApiService
import com.squareup.picasso.Picasso
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.ResponseBody
import java.lang.Exception
import java.text.SimpleDateFormat
import java.util.Locale
import java.util.TimeZone


class FighterAdapter(private val cardList: List<Matches>, private val apiService: ApiService) : RecyclerView.Adapter<ViewHolder>() {
    private val disposables = CompositeDisposable()

    class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_fighter_card, parent, false)
        return ViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val currentItem = cardList[position]

        holder.imageLeftTitle.text = currentItem.fighters[0]?.name
        Picasso.get()
            .load(Config.BASE_URL + "/v1/fighter/image/" + currentItem.fighters[0].filename)
            .placeholder(R.drawable.ic_logo_header)
            .into(holder.imageLeft);

        //------------------------------Right
        try {
            Picasso.get()
                .load(Config.BASE_URL + "/v1/fighter/image/" + currentItem.fighters[1].filename)
                .placeholder(R.drawable.ic_logo_header)
                .into(holder.imageRight)
        } catch (e: Exception) {
            // Handle the exception, maybe log it or show a user-friendly error message
            Log.e("ImageLoadError", "Error loading image", e)
            // Optionally set a fallback image in case of error
            Picasso.get()
                .load(R.drawable.ic_logo_header)
                .into(holder.imageRight)
        }


        //---------------------------------------

        holder.date.text = currentItem.date
        holder.title.text = currentItem.title
    }

    override fun getItemCount() = cardList.size

    // Holds the views for adding it to image and text
    inner class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
        val imageLeft: ImageView = itemView.findViewById(R.id.imageLeft)
        val imageLeftTitle: TextView = itemView.findViewById(R.id.titleLeft)
        val imageRight: ImageView = itemView.findViewById(R.id.imageRight)
        val imageRightTitle: TextView = itemView.findViewById(R.id.titleRight)
        val date: TextView = itemView.findViewById(R.id.tvDate)
        val title: TextView = itemView.findViewById(R.id.titleMatch)
        val favorite: ImageView = itemView.findViewById(R.id.bookmarkToggleButton)
    }

    private fun parseAndFormatDate(dateString: String): String {
        // Create a SimpleDateFormat for parsing the date string
        val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
        parser.timeZone = TimeZone.getTimeZone("UTC")

        // Parse the date
        val date = parser.parse(dateString)

        // Create a SimpleDateFormat for formatting the date
        val formatter = SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault())
        formatter.timeZone = TimeZone.getDefault() // Use local timezone

        // Format the date
        return formatter.format(date)
    }
}