//
//  KeyboardTipsPopView.swift
//  AiKeyboard
//
//  Created by Destiny on 2025/5/12.
//

import UIKit

class KeyboardTipsPopView: UIView {
    
    var settingBtnClosure: (() -> ())?
    
    var backBtnClosure: (() -> ())?
    
    lazy var bgView: UIView = {
       
        let view = UIView()
        view.backgroundColor = .hexStringColor(hexString: "#000000").withAlphaComponent(0.8)
        return view
    }()
    
    lazy var mainView: UIView = {
       
        let view = UIView()
        view.layer.cornerRadius = 18
        view.backgroundColor = .white
        return view
    }()
    
    lazy var closeBtn: UIButton = {
        
        let btn = UIButton()
        btn.setImage(UIImage(named: "keyboard_close_btn"), for: .normal)
        btn.addTarget(self, action: #selector(closeBtnClickAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var titleLabel: UILabel = {
       
        let label = UILabel()
        label.text = "开启【追爱小键盘】,并【允许完全访问】"
        label.font = .systemFont(ofSize: 13, weight: .medium)
        label.textColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
        return label
    }()
    
    lazy var mainImageView: UIImageView = {
       
        let imageView = UIImageView()
        imageView.image = UIImage(named: "keyboard_tip_settings")
        return imageView
    }()
    
    lazy var settingBtn: UIButton = {
       
        let btn = UIButton()
        btn.layer.cornerRadius = 17
        btn.setTitle("去设置", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 12, weight: .medium)
        
        if let image = UIImage.gradient([UIColor.hexStringColor(hexString: "#7D46FC"), UIColor.hexStringColor(hexString: "#BC87FF")], size: CGSize(width: 103, height: 34), locations: [0, 1], direction: .horizontal) {
            
            btn.backgroundColor = UIColor(patternImage: image)
        }
        
        btn.addTarget(self, action: #selector(settingBtnClickAction), for: .touchUpInside)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func settingBtnClickAction() {
        
        settingBtnClosure?()
    }
    
    @objc func closeBtnClickAction() {
        
        self.removeFromSuperview()
        backBtnClosure?()
    }
}

extension KeyboardTipsPopView {
    
    func initUI() {
        
        self.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(mainView)
        mainView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 296, height: 209))
            make.centerX.equalToSuperview()
            make.top.equalTo(14)
        }
        
        self.mainView.addSubview(closeBtn)
        closeBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 24, height: 24))
            make.right.equalTo(-13)
            make.top.equalTo(12)
        }
        
        self.mainView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(38)
        }
        
        self.mainView.addSubview(mainImageView)
        mainImageView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 254, height: 77))
            make.top.equalTo(titleLabel.snp.bottom).offset(14)
            make.centerX.equalToSuperview()
        }
        
        self.mainView.addSubview(settingBtn)
        settingBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 103, height: 34))
            make.top.equalTo(mainImageView.snp.bottom).offset(14)
            make.centerX.equalToSuperview()
        }
    }
}
