//
//  SessionAIHeadView.swift

//
//  Created by Leon on 2025/8/20.
//
import UIKit

class SessionAIHeadView: UITableViewCell{
    
    var block: UIView!
    var effectView : UIVisualEffectView!
    var introductionLabel: LineSpaceLabel!
    var tagsStackH : UIStackView!
    var expandButton: EPIconGhostButton!
    
    var heightChangeAction: ((_ height:CGFloat)->Void)?
    
    var data: IMAIUserInfo?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = .clear
        
        setupViews()
        setupData()
        setupEvent()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
        block = {
            let v = UIView()
            v.backgroundColor = .clear
            v.cornerRadius = 16
            contentView.addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview().inset(UIEdgeInsets(top: 8, left: 24, bottom: 8, right: 24))
//                make.leading.equalToSuperview().offset(24)
//                make.trailing.equalToSuperview().offset(-24)
//                make.top.equalToSuperview().offset(8)
            }
            return v
        }()
        
        effectView = {
            let v = UIVisualEffectView(effect: UIBlurEffect(style: .light))
            v.alpha = 0.9
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()
        
        introductionLabel = {
            let v = LineSpaceLabel()
            let typo = CLSystemToken.typography(token: .tbm)
            v.config(typo)
            v.numberOfLines = 3
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(16)
                make.top.equalToSuperview().offset(16)
                make.trailing.equalToSuperview().offset(-16)
                make.bottom.equalToSuperview().offset(-48)
            }
            return v
        }()
        
        expandButton = {
            let v = EPIconGhostButton(radius: .none, iconSize: .xs, iconCode: .iconFullimage)
            v.addTarget(self, action: #selector(tapExpandButton), for: .touchUpInside)
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.trailing.equalToSuperview()
                make.bottom.equalToSuperview().offset(-8)
                make.size.equalTo(CGSize(width: 40, height: 40))
            }
            return v
        }()
        
        tagsStackH = {
            let v = UIStackView()
            v.spacing = 8
            block.addSubview(v)
            v.snp.makeConstraints { make in
                make.leading.equalToSuperview().offset(16)
                make.top.equalTo(introductionLabel.snp.bottom).offset(8)
            }
            return v
        }()
    }
    
    private func setupData(){
        
    }
    
    private func testData(){
        introductionLabel.text = "Intro: She is a new trainee teacher who has just graduated. You are the most rebellious student in the whole school. In order to prove her ability, she took the initiative to apply to be your class teacher. In the upcoming college entrance examination, you two…"
        
        do {
            let tag = RoleTag()
            tag.title = "Sensibility"
            tag.style = .purple
            tagsStackH.addArrangedSubview(tag)
        }
        
        do {
            let tag = RoleTag()
            tag.title = "Romantic"
            tag.style = .theme
            tagsStackH.addArrangedSubview(tag)
        }
    }
    
    private func setupEvent(){
        
    }
    
    func refresh(_ user: IMAIUserInfo?){
        self.data = user
        
        guard let info = user else {return}
        
        introductionLabel.text = info.introduction
        
        tagsStackH.removeSubviews()
        if let character = info.characterName{
            let tag = RoleTag()
            tag.title = character
            tag.style = .blurPurple
            tagsStackH.addArrangedSubview(tag)
        }
        
        if let tagName = info.tagName{
            let tag = RoleTag()
            tag.title = tagName
            tag.style = .blurTheme
            tagsStackH.addArrangedSubview(tag)
        }
    }
    
    // MARK: Action
    
    @objc private func tapExpandButton(){
        guard let introduction = data?.introduction else{return}
        
        let vc = RoleIntroBrowseDialogController()
        vc.introduction = introduction
        vc.imageUrl = data?.backgroundImg
        vc.show()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let maxY = block.frame.maxY + 8
        heightChangeAction?(maxY)
    }
}
