//
//  CLTopHeaderView.swift
//  Visual_Novel_iOS
//
//  Created by mh on 2025/10/15.
//

import Foundation
import Combine

/// 所有可能跳转的目标页面
enum JumpTarget: CaseIterable {
    case search
    case check // 签到
    case discord
//    case web(url: String) // 关联值可以带参数
}


/// tabbar上rootvc顶部头像等公共区域
class CLTopHeaderView: UIView {
    
    private let subject = PassthroughSubject<JumpTarget, Never>()
    var jumpPublisher: AnyPublisher<JumpTarget, Never> {
        subject.eraseToAnyPublisher()
    }
    
    lazy var avatarContainerView: UIView = {
        let view = UIView()
        return view
    }()
    
    lazy var avatar: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.cornerRadius = 18.0
        imgView.backgroundColor = .purple
        return imgView
    }()
    
    lazy var vipImgView: UIImageView = {
        let imgview = UIImageView(image: UIImage(named: "header_not_subscriber_icon"))
        imgview.contentMode = .scaleAspectFill
        return imgview
    }()
    
    lazy var containerView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.cornerRadius = 18.0
        return view
    }()
    
    lazy var starImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "header_money_icon"))
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()
    
    lazy var moneyLab: UILabel = {
        let lab = UILabel()
        lab.text = "150"
        lab.font = UIFont.boldSystemFont(ofSize: 14)
        return lab
    }()
    
    lazy var searchImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "header_search_icon"))
        imgView.contentMode = .scaleAspectFill
        imgView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(searchImgViewClicked))
        imgView.addGestureRecognizer(tap)
        return imgView
    }()
    
    lazy var calendarImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "header_check_icon"))
        imgView.contentMode = .scaleAspectFill
        imgView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(calendarImgViewClicked))
        imgView.addGestureRecognizer(tap)
        return imgView
    }()
    
    lazy var discordImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "header_discord_icon"))
        imgView.contentMode = .scaleAspectFill
        imgView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(discordImgViewClicked))
        imgView.addGestureRecognizer(tap)
        return imgView
    }()
    
    lazy var avatarStackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [avatarContainerView, containerView])
        stackView.axis = .horizontal
        stackView.spacing = 15.0
        stackView.alignment = .fill
        stackView.distribution = .fill
        return stackView
    }()
    
    lazy var toolStackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [searchImgView, calendarImgView, discordImgView])
        stackView.axis = .horizontal
        stackView.spacing = 15.0
        stackView.alignment = .fill
        stackView.distribution = .fill
        return stackView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupSubviews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: Action
    @objc func searchImgViewClicked() {
        subject.send(.search)
    }
    
    @objc func calendarImgViewClicked() {
        subject.send(.check)
    }
    
    @objc func discordImgViewClicked() {
        subject.send(.discord)
    }
    
    // MARK: Subviews
    func setupSubviews() {
        
        addSubview(avatarStackView)
        addSubview(toolStackView)

        avatarContainerView.addSubview(avatar)
        avatarContainerView.addSubview(vipImgView)
        
        containerView.addSubview(starImgView)
        containerView.addSubview(moneyLab)
        
        avatarStackView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(UIDevice().statusBarHeight + 4.0)
        }
        
        toolStackView.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(20)
            make.centerY.equalTo(avatarStackView.snp.centerY)
        }
        
        avatarContainerView.snp.makeConstraints { make in
            make.width.height.equalTo(36)
        }
        
        avatar.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        vipImgView.snp.makeConstraints { make in
            make.right.bottom.equalToSuperview()
        }
        
        containerView.snp.makeConstraints { make in
            make.height.equalTo(36)
        }
        
        starImgView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(6.5)
            make.centerY.equalToSuperview()
        }
        
        moneyLab.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(starImgView.snp.right).offset(5.5)
            make.right.equalToSuperview().inset(12.5)
        }
    }
}
