package com.mistral.chat.ui

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.mistral.chat.R
import com.mistral.chat.data.Message
import io.noties.markwon.Markwon
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin
import io.noties.markwon.ext.tables.TablePlugin
import io.noties.markwon.ext.tasklist.TaskListPlugin

class MessageAdapter(private val messages: List<Message>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun getItemViewType(position: Int): Int {
        return if (messages[position].isUser) 0 else 1
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == 0) {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_message_user, parent, false)
            UserMessageHolder(view)
        } else {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_message_assistant, parent, false)
            AssistantMessageHolder(view)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val message = messages[position]
        when (holder) {
            is UserMessageHolder -> holder.bind(message)
            is AssistantMessageHolder -> holder.bind(message)
        }
    }

    override fun getItemCount(): Int = messages.size

    class UserMessageHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(message: Message) {
            val textView = itemView.findViewById<TextView>(R.id.messageText)
            textView.text = message.content
            textView.setBackgroundResource(R.drawable.bg_message_user)
            textView.setTextColor(ContextCompat.getColor(itemView.context, R.color.user_message_text))
            
            itemView.setOnLongClickListener {
                copyToClipboard(itemView.context, message.content)
                true
            }
        }
    }

    class AssistantMessageHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val markwon: Markwon = Markwon.builder(itemView.context)
            .usePlugin(StrikethroughPlugin.create())
            .usePlugin(TablePlugin.create(itemView.context))
            .usePlugin(TaskListPlugin.create(itemView.context))
            .build()

        fun bind(message: Message) {
            val senderNameView = itemView.findViewById<TextView>(R.id.senderName)
            val textView = itemView.findViewById<TextView>(R.id.messageText)
            
            senderNameView.text = message.senderName ?: "Assistant"
            markwon.setMarkdown(textView, message.content)
            textView.setBackgroundResource(R.drawable.bg_message_assistant)
            textView.setTextColor(ContextCompat.getColor(itemView.context, R.color.assistant_message_text))
            
            itemView.setOnLongClickListener {
                copyToClipboard(itemView.context, message.content)
                true
            }
        }
    }

    companion object {
        fun copyToClipboard(context: Context, text: String) {
            val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val clip = ClipData.newPlainText("Message", text)
            clipboard.setPrimaryClip(clip)
            Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show()
        }
    }
}