//
//  SessionCoverView.swift
//  Visual_Novel_iOS
//
//  Created by mh on 2025/10/21.
//

import UIKit

class SessionCoverView: UIView {
    
    var aiAnswerBlock: (()->Void)? = nil
    
    lazy var expandBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "role_chat_expand_open"), for: .normal)
        btn.setImage(UIImage(named: "role_chat_expand_close"), for: .selected)
        btn.backgroundColor = .clear
        btn.isUserInteractionEnabled = false
        return btn
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
//        view.backgroundColor = UIColor.init(white: 1.0, alpha: 0.2)
        view.backgroundColor = .red
        view.cornerRadius = 0.5
        view.alpha = 0
        return view
    }()
    
    lazy var phoneBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "role_chat_expand_phone"), for: .normal)
        btn.addTarget(self, action: #selector(phoneBtnClicked), for: .touchDown)
        btn.alpha = 0
        return btn
    }()
    
    let vipImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "role_chat_phone_vip"))
        return imgView
    }()
    
    lazy var aiBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "role_chat_ai"), for: .normal)
        btn.addTarget(self, action: #selector(aiBtnClicked), for: .touchDown)
        btn.alpha = 0
        return btn
    }()
    
    lazy var bgview: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(bgviewTap)))
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func bgviewTap() {
        print("bgviewTap-bgviewTap")
    }
    
    @objc func phoneBtnClicked() {
        self.aiAnswerBlock?()
    }
    
    @objc func aiBtnClicked() {
        self.aiAnswerBlock?()
    }
    
    func expandStatusChanged(_ expand: Bool) {
        expandBtn.isSelected = expand
        self.backgroundColor = expand ? .orange : .blue
        UIView.animate(withDuration: 0.25) {
            self.phoneBtn.alpha = expand ? 1 : 0
            self.aiBtn.alpha = expand ? 1 : 0
            self.lineView.alpha = expand ? 1 : 0
        }
    }
    
    private func setupUI() {
        
        addSubview(expandBtn)
        addSubview(lineView)
        addSubview(bgview)
        addSubview(phoneBtn)
        addSubview(vipImgView)

        addSubview(aiBtn)
        
        expandBtn.snp.makeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(44)
        }
        
        lineView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.equalTo(1.0)
            make.width.equalTo(10)
            make.bottom.equalTo(expandBtn.snp.top)
        }
        
        phoneBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(lineView.snp.top).offset(-17)
            make.width.height.equalTo(30)
        }
        
        vipImgView.snp.makeConstraints { make in
            make.right.equalTo(phoneBtn.snp.right)
            make.bottom.equalTo(phoneBtn.snp.bottom)
        }
        
        aiBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(phoneBtn.snp.top).offset(-20)
            make.width.height.equalTo(30)
        }
        
        bgview.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.bottom.equalTo(lineView.snp.top)
        }
    }
}
