//
//  QSLMineVipView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2024/4/15.
//

import UIKit

protocol QSLMineVipViewDelegate: NSObjectProtocol {
    
    func unlockBtnAction()
}

class QSLMineVipView: UIView {
    
    struct UX {
        static let buttonWidth = (QSLConst.qsl_kScreenW - 24.0) / 3
        static let buttonHeight = 32.0
    }
    
    weak var delegate: QSLMineVipViewDelegate?
    
    lazy var vipBgImageView: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "mine_vip_bg")
        return imageView
    }()
    
    lazy var vipTitleIcon: UIImageView = {
        
        let imageView = UIImageView()
        imageView.image = UIImage(named: "mine_vip_title")
        return imageView
    }()
    
    lazy var vipContentLabel: UILabel = {
        
        let label = UILabel()
        label.text("升级VIP会员，解锁全部功能")
        label.font(13)
        label.textColor = .hexStringColor(hexString: "#FFFFFF", alpha: 0.8)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(contentLabelAction))
        label.isUserInteractionEnabled = true
        label.addGestureRecognizer(tap)
        
        return label
    }()
    
    lazy var vipUnlockButton: UIButton = {
       
        let btn = UIButton()
        btn.addRadius(radius: 4.rpx)
        btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#FFE1CD"), color2: .hexStringColor(hexString: "#FEB484"), width: 80.rpx, height: 32.rpx, direction: .horizontal)
        btn.title("立即开通")
        btn.textColor(.hexStringColor(hexString: "#9B3800"))
        btn.boldFont(14)
        btn.addTarget(self, action: #selector(unlockBtnAction), for: .touchUpInside)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension QSLMineVipView {
    
    @objc func unlockBtnAction() {
        delegate?.unlockBtnAction()
    }
    
    @objc func contentLabelAction() {
        
        let model = QSLBaseManager.shared.userModel.memberModel
        if model.permanent {
            delegate?.unlockBtnAction()
        }
    }
}

// MARK: - UI相关
extension QSLMineVipView {
    
    func updateUI() {
        
        if QSLBaseManager.shared.isLogin() && QSLBaseManager.shared.isVip() {
            let model = QSLBaseManager.shared.userModel.memberModel
            if model.permanent {
                
                self.vipUnlockButton.isHidden = true
                self.vipContentLabel.text("您已是尊贵的永久会员 >")
            } else {
                
                self.vipUnlockButton.isHidden = false
                let level = model.memberLevelString()
                let endTime = model.endTimestampString()
                self.vipUnlockButton.title("立即续费")
                self.vipContentLabel.text("\(level)：\(endTime)到期")
            }
            
        } else {
            
            self.vipUnlockButton.isHidden = false
            self.vipUnlockButton.title("立即开通")
            self.vipContentLabel.text("升级VIP会员，解锁全部功能")
        }
    }
    
    func setUpUI() {
        
        addSubview(vipBgImageView)
        addSubview(vipContentLabel)
        addSubview(vipTitleIcon)
        addSubview(vipUnlockButton)
        
        vipBgImageView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
        
        vipTitleIcon.snp.makeConstraints { make in
            make.left.equalTo(17.rpx)
            make.bottom.equalTo(self.snp.centerY)
            make.size.equalTo(CGSize(width: 65.5.rpx, height: 17.rpx))
        }
        
        vipContentLabel.snp.makeConstraints { make in
            make.left.equalTo(17.rpx)
            make.top.equalTo(vipTitleIcon.snp.bottom).offset(5.rpx)
        }
        
        vipUnlockButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 80.rpx, height: 32.rpx))
            make.centerY.equalToSuperview()
            make.right.equalTo(-20.rpx)
        }
        
    }
    
}
