//
//  ChatSwipeCell.swift
//  Visual_Novel_iOS
//
//  Created by mh on 2025/10/27.
//

import UIKit
import SnapKit

//  icon title 声音头像 箭头
struct ImageRow: RowModel {
    let icon: String
    let title: String
    let showAvatar: Bool
    let showArrow: Bool
    let showSwitch: Bool
    let subItems: [ImageRow]? // 子项列表
    let buttleItems: [ChatButtleRow]?
    let chatModeItems: [ChatModeItem]?
    let voiceActorItems: [VoiceActorItem]?
    var isExpanded: Bool = false // 展开状态
    
    var cellReuseID: String { "ChatSwipeCell" }
    func cellHeight(tableWidth: CGFloat) -> CGFloat { 50 }
    
    init(icon: String, title: String, showAvatar: Bool = false, showArrow: Bool = false, showSwitch: Bool = false, subItems: [ImageRow]? = nil, buttleItems: [ChatButtleRow]? = nil, chatModeItems: [ChatModeItem]? = nil, voiceActorItems: [VoiceActorItem]? = nil) {
        self.icon = icon
        self.title = title
        self.showAvatar = showAvatar
        self.showArrow = showArrow
        self.showSwitch = showSwitch
        self.subItems = subItems
        self.buttleItems = buttleItems
        self.chatModeItems = chatModeItems
        self.voiceActorItems = voiceActorItems
    }
}

class ChatSwipeCell: ChatSettingBaseCell, CellConfigurable {
    
    lazy var iconImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "role_exchange_mode"))
        return imgView
    }()
    
    lazy var titleLab: UILabel = {
        let lab = UILabel()
        lab.text = "XL-0826-32K"
        lab.font = UIFont.boldSystemFont(ofSize: 14)
        lab.textColor = UIColor(hex: "#666666")
        return lab
    }()
    
    var avatarView : UIImageView = {
        let imgView = UIImageView()
        imgView.cornerRadius = 10.5
        imgView.backgroundColor = .blue
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        return imgView
    }()
    
    lazy var arrowImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "role_setting_go"))
        return imgView
    }()
    
    lazy var switchControl: SevenSwitch = {
        let con = SevenSwitch()
        con.onTintColor = UIColor(hex: "#020025")
        con.onThumbTintColor = UIColor(hex: "#00CC88")
        con.inactiveColor = UIColor(hex: "#020025")
        return con
    }()
    
    lazy var stackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [avatarView, arrowImgView])
        stackView.spacing = 5
        stackView.distribution = .fill
        stackView.alignment = .center
        return stackView
    }()
    
    private var currentRow: ImageRow?
    
    func configure(with row: RowModel) {
        guard let row = row as? ImageRow else { return }
        currentRow = row
        iconImgView.image = UIImage(named: row.icon)
        titleLab.text = row.title
        avatarView.isHidden = !row.showAvatar
        arrowImgView.isHidden = !row.showArrow
        switchControl.isHidden = !row.showSwitch
        
        // 如果有 voice actor items，设置默认选中的头像
        if row.showAvatar, let voiceActorItems = row.voiceActorItems,
           let selectedItem = voiceActorItems.first(where: { $0.isSelected }) {
            avatarView.image = UIImage(named: selectedItem.avatarImage)
        }
        
        // 更新箭头状态
        updateArrowState(isExpanded: row.isExpanded)
        
        // 更新container高度（固定为50）
        updateContainerHeight(50)
    }
    
    func updateArrowState(isExpanded: Bool) {
        UIView.animate(withDuration: 0.3) { [weak self] in
            self?.arrowImgView.transform = isExpanded ? CGAffineTransform(rotationAngle: .pi / 2.0) : .identity
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configureViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     func configureViews() {
        containerView.addSubview(iconImgView)
        containerView.addSubview(titleLab)
        containerView.addSubview(stackView)
        containerView.addSubview(switchControl)
    
        iconImgView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(12)
            make.width.height.equalTo(21)
        }
        
        avatarView.snp.makeConstraints { make in
            make.width.height.equalTo(21)
        }
        
        stackView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(15)
        }
        
        titleLab.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(iconImgView.snp.right).offset(9)
            make.right.equalTo(stackView.snp.left).offset(-5)
        }
        
        switchControl.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(15)
            make.width.equalTo(45)
            make.height.equalTo(23)
        }
        
        titleLab.setContentCompressionResistancePriority(.fittingSizeLevel, for: .horizontal)
        titleLab.setContentHuggingPriority(.fittingSizeLevel, for: .horizontal)
    }
}
