package com.aiosman.ravenow.ui.agent

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.aiosman.ravenow.data.AccountService
import com.aiosman.ravenow.data.AccountServiceImpl
import com.aiosman.ravenow.data.UploadImage
import com.aiosman.ravenow.data.ServiceException
import com.aiosman.ravenow.entity.AccountProfileEntity
import com.aiosman.ravenow.entity.AgentEntity
import com.aiosman.ravenow.entity.createAgent
import com.aiosman.ravenow.ui.index.tabs.profile.MyProfileViewModel
import com.aiosman.ravenow.utils.TrtcHelper
import java.io.File

object AddAgentViewModel : ViewModel() {
    var name by mutableStateOf("")
    var desc by mutableStateOf("")
    var croppedBitmap by mutableStateOf<Bitmap?>(null)
    var isUpdating by mutableStateOf(false)
    var isSelectingAvatar by mutableStateOf(false) // 标记是否正在选择头像
    var showManualCreationForm by mutableStateOf(false)
    var isCreatingAgent by mutableStateOf(false)
    var showWaveAnimation by mutableStateOf(false)
    var showManualCreation by mutableStateOf(false)
    // 添加一个标志来区分两种手动表单状态
    var isAutoModeManualForm by mutableStateOf(false)
    suspend fun updateAgentAvatar(context: Context) {
        croppedBitmap?.let {
            val file = File(context.cacheDir, "agent_avatar.jpg")
            it.compress(Bitmap.CompressFormat.JPEG, 100, file.outputStream())
            // 这里可以上传图片到服务器，暂时先保存到本地
            // UploadImage(file, "agent_avatar.jpg", "", "jpg")
        }
    }

    suspend fun createAgent(context: Context): AgentEntity? {
        try {
            isUpdating = true
            
            // 准备头像文件
            val avatarFile = if (croppedBitmap != null) {
                val file = File(context.cacheDir, "agent_avatar.jpg")
                croppedBitmap!!.compress(Bitmap.CompressFormat.JPEG, 100, file.outputStream())
                UploadImage(file, "agent_avatar.jpg", "", "jpg")
            } else {
                null
            }
            
            // 调用API创建智能体
            val result = createAgent(
                title = name,
                desc = desc,
                avatar = avatarFile
            )
            
            return result
        } catch (e: Exception) {
            throw e
        } finally {
            isUpdating = false
        }
    }
    


    fun validate(): String? {
        return when {
            name.isEmpty() -> "智能体名称不能为空"
            name.length < 2 -> "智能体名称长度不能少于2个字符"
            name.length > 20 -> "智能体名称长度不能超过20个字符"
            desc.isEmpty() -> "智能体描述不能为空"
            desc.length > 512 -> "智能体描述长度不能超过512个字符"
            else -> null
        }
    }
    
    /**
     * 清空所有页面数据
     */
    fun clearData() {
        name = ""
        desc = ""
        croppedBitmap = null
        isUpdating = false
        isSelectingAvatar = false
        showManualCreationForm = false
        isCreatingAgent = false
        showWaveAnimation = false
        showManualCreation = false
        isAutoModeManualForm = false
    }
}