//
//  TSMineTopView.swift
//  KittensTravelNotes
//
//  Created by 100Years on 2025/7/15.
//

class TSSetingTopView: TSBaseView {
    
    var clickUpgrade:(()->Void)?
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel.createLabel(text: "Update to PRO".localized, font: .font(name: .CherryBombOneRegular,size: 22,weight: .bold), textColor: .mainText)
        return titleLabel
    }()
    
    lazy var timeLabel: UILabel = {
        let timeLabel = UILabel.createLabel(text: "Limited Time Discount".localized, font: .font(size: 14), textColor: .lesserText)
        return timeLabel
    }()
    lazy var upgradeBtn: UIButton = {
        let upgradeBtn = UIButton.createButton(title: "Upgrade".localized,backgroundColor: .themeColor,font: .font(size: 14,weight: .medium),titleColor: .mainText,corner: 13)
        upgradeBtn.isUserInteractionEnabled = false
        upgradeBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
        return upgradeBtn
    }()
    
    lazy var noVipView: UIView = {
        let contentView = UIView()
        let vipBgImageView = UIImageView.createImageView(image: .settingPurchaseBg, contentMode: .scaleToFill,backgroundColor: .white)
        contentView.addSubview(vipBgImageView)
        vipBgImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.leading.trailing.bottom.equalTo(0)
        }
        
        contentView.addSubview(upgradeBtn)
        upgradeBtn.snp.makeConstraints { make in
            make.height.equalTo(26)
            make.trailing.equalTo(-14)
            make.centerY.equalToSuperview()
        }
    
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.top.equalTo(20)
        }

        contentView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.bottom.equalTo(-20)
        }

        return contentView
    }()
    

    
    lazy var vipTitleLabel: UILabel = {
        let titleLabel = UILabel.createLabel(text: kAppName + " PRO", font: .font(name: .CherryBombOneRegular,size: 22,weight: .bold), textColor: .mainText)
        return titleLabel
    }()
    
    lazy var vipTimeLabel: UILabel = {
        let timeLabel = UILabel.createLabel(text: "Limited time discount".localized, font: .font(size: 14), textColor: .lesserText)
        return timeLabel
    }()
    
    
    lazy var vipView: UIView = {
        let contentView = UIView()
        let vipBgImageView = UIImageView.createImageView(image: .settingPurchaseVipBg, contentMode: .scaleToFill,backgroundColor: .white)
        contentView.addSubview(vipBgImageView)
        vipBgImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.leading.trailing.bottom.equalTo(0)
        }
        
        contentView.addSubview(vipTitleLabel)
        vipTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.leading.equalTo(16)
        }

        contentView.addSubview(vipTimeLabel)
        vipTimeLabel.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.bottom.equalTo(-20)
        }
        
        contentView.isHidden = true
        return contentView
    }()
    
    @objc func clickView(){
        clickUpgrade?()
    }
    override func creatUI() {
        
        contentView.cornerRadius = 20
        contentView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickView)))
        contentView.snp.updateConstraints { make in
            make.bottom.equalTo(-16)
        }
        
        contentView.addSubview(noVipView)
        noVipView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(vipView)
        vipView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        updateVipView()
    }
    
    func updateVipView() {
        if PurchaseManager.default.isVip {
            noVipView.isHidden = true
            vipView.isHidden = false
            vipTimeLabel.text = "Due Date:".localized + "\(PurchaseManager.default.expiredDateString)"
        } else {
            noVipView.isHidden = false
            vipView.isHidden = true
        }
    }
}
