//
//  KeyboardVipTipView.swift
//  AiKeyboard
//
//  Created by Destiny on 2025/4/25.
//

import UIKit
import Lottie

class KeyboardVipTipView: UIView {
    
    lazy var bgImageView: UIImageView = {
        
        let imageView = UIImageView()
        imageView.image = UIImage(named: "keyboard_bg")
        return imageView
    }()
    
    lazy var backBtn: UIButton = {
        
        let btn = UIButton()
        btn.setImage(UIImage(named: "keyboard_back_btn"), for: .normal)
        btn.layer.shadowOffset = CGSize(width: 0, height: 0)
        btn.layer.shadowColor = UIColor.hexStringColor(hexString: "#000000", alpha: 0.12).cgColor
        btn.layer.shadowRadius = 6.1
        btn.addTarget(self, action: #selector(backButtonClickAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var topImageView: UIImageView = {
        
        let imageView = UIImageView()
        imageView.image = UIImage(named: "keyboard_vip_top")
        return imageView
    }()
    
    lazy var mainView: UIView = {
      
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = 20
        return view
    }()
    
    lazy var mainLabel: UILabel = {
       
        let label = UILabel()
        label.numberOfLines = 2
        label.text = "您的免费提问次数已用完\n可订阅会员解锁无线提问"
        label.font = .systemFont(ofSize: 14, weight: .medium)
        label.textColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
        return label
    }()
    
    lazy var unlockBtnAnimation: LottieAnimationView = {
       
        let animate = LottieAnimationView(name: "vip_button")
        animate.loopMode = .loop
        animate.backgroundColor = .clear
        return animate
    }()
    
    lazy var unlockLabel: UILabel = {
        
        let name = self.getFontPostScriptName(from: "淘宝买菜体.ttf")
        let label = UILabel()
        label.text = "￥0.1/天解锁"
        label.font = UIFont(name: name ?? "", size: 24)
        label.textColor = .white
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func getFontPostScriptName(from fontFile: String) -> String? {
        guard let path = Bundle.main.path(forResource: fontFile, ofType: nil) else {
            return nil
        }
        let fontURL = URL(fileURLWithPath: path)
        guard let fontDataProvider = CGDataProvider(url: fontURL as CFURL),
              let font = CGFont(fontDataProvider) else {
            return nil
        }
        return font.postScriptName as String?
    }
}

extension KeyboardVipTipView {
    
    @objc func backButtonClickAction() {
        
        self.removeFromSuperview()
    }
}

extension KeyboardVipTipView {
    
    func setUI() {
        
        self.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(backBtn)
        backBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 32, height: 32))
            make.left.equalTo(12)
            make.top.equalTo(14)
        }
        
        self.addSubview(topImageView)
        topImageView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 264, height: 98.5))
            make.top.equalTo(47)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(mainView)
        mainView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 285, height: 122))
            make.centerX.equalToSuperview()
            make.top.equalTo(topImageView.snp.bottom).offset(-9)
        }
        
        mainView.addSubview(mainLabel)
        mainLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(21)
        }
        
        self.addSubview(unlockBtnAnimation)
        unlockBtnAnimation.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 300, height: 48))
            make.centerX.equalToSuperview()
            make.top.equalTo(mainView.snp.bottom).offset(-30)
        }
        unlockBtnAnimation.play()
        
        unlockBtnAnimation.addSubview(unlockLabel)
        unlockLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
}
