//
//  SubItemsContainerCell.swift
//  Visual_Novel_iOS
//
//  Created by mh on 2025/10/30.
//

import UIKit
import SnapKit

// switch model中点击展开的item
// SubItemsRow - 用于标识子项容器cell
struct SubItemsRow: RowModel {
    let subItems: [ImageRow]
    var cellReuseID: String { "SubItemsContainerCell" }
    func cellHeight(tableWidth: CGFloat) -> CGFloat {
        let maxDisplayCount = 4
        let displayCount = min(subItems.count, maxDisplayCount)
        return CGFloat(displayCount) * 58 // 每行40高度
    }
}

// SubItemsContainerCell - 子项容器cell
class SubItemsContainerCell: UITableViewCell, CellConfigurable {
    
    private var subItems: [ImageRow] = []
    private var tableViewHeightConstraint: Constraint?
    
    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 = true
        tableView.isScrollEnabled = true
        tableView.register(SubItemCell.self, forCellReuseIdentifier: "SubItemCell")
        tableView.estimatedRowHeight = 58
        tableView.rowHeight = 58
        tableView.contentInset = .zero
        tableView.scrollIndicatorInsets = .zero
        tableView.showsVerticalScrollIndicator = false
        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 subItemsRow = row as? SubItemsRow else { return }
        subItems = subItemsRow.subItems
        
        tableView.reloadData()
        updateTableViewHeight()
    }
    
    private func updateTableViewHeight() {
        let maxDisplayCount = 4
        let displayCount = min(subItems.count, maxDisplayCount)
        let height = CGFloat(displayCount) * 58
        
        tableViewHeightConstraint?.update(offset: height)
        layoutIfNeeded()
    }
}

extension SubItemsContainerCell: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return subItems.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SubItemCell", for: indexPath) as! SubItemCell
        let subItem = subItems[indexPath.row]
        cell.configure(with: subItem)
        cell.lineView.isHidden = subItems.count - 1 == indexPath.row
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}

// SubItemCell - 子项cell
class SubItemCell: UITableViewCell {
    private lazy var titleLabel: UILabel = {
        let lab = UILabel()
        lab.text = "Max-0618"
        lab.textColor = UIColor(hex: "#333333")
        lab.font = UIFont.systemFont(ofSize: 13)
        return lab
    }()
    
    private lazy var iconImageView: UIImageView = {
        let view = UIImageView()
        view.cornerRadius = 12.5
        view.backgroundColor = .darkText
        return view
    }()
    
    lazy var subTitleLab: UILabel = {
        let lab = UILabel()
        lab.text = "Previous-generation large model"
        lab.textColor = UIColor(hex: "#9494C3")
        lab.font = UIFont.systemFont(ofSize: 10)
        return lab
    }()
    
    lazy var tokenLab: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor(hex: "#0066FF")
        lab.text = "0.3 points / 1K tokens (Recommended)"
        lab.font = UIFont.systemFont(ofSize: 11)
        return lab
    }()
    
    lazy var statusImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: ""))
        imgView.backgroundColor = .blue
        return imgView
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#ECECF9")
        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(iconImageView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(subTitleLab)
        contentView.addSubview(tokenLab)
        contentView.addSubview(statusImgView)
        contentView.addSubview(lineView)
        
        iconImageView.snp.makeConstraints { make in
            make.left.top.equalToSuperview().offset(10)
            make.width.height.equalTo(25)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(8)
            make.top.equalTo(iconImageView.snp.top).offset(-2)
            make.right.equalTo(statusImgView.snp.left).inset(-10)
        }
        
        subTitleLab.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.left)
            make.top.equalTo(titleLabel.snp.bottom)
            make.right.equalTo(titleLabel.snp.right)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        tokenLab.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.left)
            make.right.equalTo(titleLabel.snp.right)
            make.bottom.equalToSuperview().inset(5)
        }
        
        statusImgView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(10)
            make.width.height.equalTo(13)
        }
    }
    
    func configure(with item: ImageRow) {
        iconImageView.image = UIImage(named: item.icon)
        titleLabel.text = item.title
    }
}

