//
//  ChatModeContainerCell.swift
//  Visual_Novel_iOS
//
//  Created by mh on 2025/01/27.
//

import UIKit
import SnapKit

// MARK: - 数据模型
struct ChatModeItem {
    let id: String
    let title: String
    let subtitle: String
    let isVip: Bool
    var isSelected: Bool
}

// MARK: - RowModel
struct ChatModeRow: RowModel {
    let items: [ChatModeItem]
    var cellReuseID: String { "ChatModeContainerCell" }
    
    func cellHeight(tableWidth: CGFloat) -> CGFloat {
        let rowHeight: CGFloat = 58
        return CGFloat(items.count) * rowHeight
    }
}

// MARK: - ChatModeContainerCell
class ChatModeContainerCell: UITableViewCell, CellConfigurable {
    
    private var items: [ChatModeItem] = []
    private var selectedItemId: String?
    private var tableViewHeightConstraint: Constraint?
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor(hex: "#F5F5FF")
        tableView.showsVerticalScrollIndicator = false
        tableView.isScrollEnabled = false
        tableView.register(ChatModeItemCell.self, forCellReuseIdentifier: "ChatModeItemCell")
        tableView.estimatedRowHeight = 58
        tableView.rowHeight = 58
        tableView.contentInset = .zero
        tableView.scrollIndicatorInsets = .zero
        tableView.bounces = false
        tableView.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
        tableView.layer.cornerRadius = 15
        return tableView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupViews() {
        backgroundColor = .clear
        selectionStyle = .none
        
        contentView.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.top.bottom.equalToSuperview().inset(2.5)
            tableViewHeightConstraint = make.height.equalTo(0).constraint
        }
    }
    
    func configure(with row: RowModel) {
        guard let modeRow = row as? ChatModeRow else { return }
        items = modeRow.items
        
        // 找到选中的item
        if let selected = items.first(where: { $0.isSelected }) {
            selectedItemId = selected.id
        }
        
        tableView.reloadData()
        updateTableViewHeight()
    }
    
    private func updateTableViewHeight() {
        let rowHeight: CGFloat = 58
        let totalHeight = CGFloat(items.count) * rowHeight
        
        tableViewHeightConstraint?.update(offset: totalHeight)
        layoutIfNeeded()
    }
}

// MARK: - UITableViewDataSource & UITableViewDelegate
extension ChatModeContainerCell: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ChatModeItemCell", for: indexPath) as! ChatModeItemCell
        let item = items[indexPath.row]
        let isSelected = item.id == selectedItemId
        cell.configure(with: item, isSelected: isSelected)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let item = items[indexPath.row]
        
        // 更新选中状态
        selectedItemId = item.id
        
        // 重新加载所有cell以更新选中状态
        tableView.reloadData()
    }
}

// MARK: - ChatModeItemCell
class ChatModeItemCell: UITableViewCell {
    
    private lazy var containerView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#F5F5FF")
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor(hex: "#333333")
        lab.font = UIFont.systemFont(ofSize: 13)
        return lab
    }()
    
    private lazy var vipBadge: UILabel = {
        let lab = UILabel()
        lab.text = "VIP"
        lab.textColor = .black
        lab.font = UIFont.systemFont(ofSize: 10, weight: .bold)
        lab.backgroundColor = UIColor(hex: "#FFD700")
        lab.textAlignment = .center
        lab.layer.cornerRadius = 6
        lab.clipsToBounds = true
        lab.isHidden = true
        return lab
    }()
    
    private lazy var subtitleLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor(hex: "#9494C3")
        lab.font = UIFont.systemFont(ofSize: 11)
        return lab
    }()
    
    private lazy var selectionIndicator: UIView = {
        let view = UIView()
        view.layer.cornerRadius = 10
        view.backgroundColor = UIColor(hex: "#333333")
        return view
    }()
    
    private lazy var selectedDot: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#00CC88")
        view.layer.cornerRadius = 5
        view.isHidden = true
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupViews() {
        backgroundColor = .clear
        selectionStyle = .none
        
        contentView.addSubview(containerView)
        containerView.addSubview(titleLabel)
        containerView.addSubview(vipBadge)
        containerView.addSubview(subtitleLabel)
        containerView.addSubview(selectionIndicator)
        selectionIndicator.addSubview(selectedDot)
        
        containerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.top.bottom.equalToSuperview().inset(2.5)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(8)
            make.right.lessThanOrEqualTo(vipBadge.snp.left).offset(-8)
        }
        
        vipBadge.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right).offset(8)
            make.centerY.equalTo(titleLabel)
            make.width.equalTo(24)
            make.height.equalTo(14)
        }
        
        subtitleLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
            make.right.lessThanOrEqualTo(selectionIndicator.snp.left).offset(-10)
        }
        
        selectionIndicator.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(10)
            make.width.height.equalTo(20)
        }
        
        selectedDot.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(10)
        }
    }
    
    func configure(with item: ChatModeItem, isSelected: Bool) {
        titleLabel.text = item.title
        subtitleLabel.text = item.subtitle
        vipBadge.isHidden = !item.isVip
        
        // 更新选中状态
        if isSelected {
            selectionIndicator.backgroundColor = UIColor(hex: "#00CC88")
            selectedDot.isHidden = false
        } else {
            selectionIndicator.backgroundColor = UIColor(hex: "#333333")
            selectedDot.isHidden = true
        }
    }
}
