//
//  YHPrivacyAlertView.swift
//  galaxy
//
//  Created by EDY on 2024/1/12.
//  Copyright © 2024 www.davidhuang.com. All rights reserved.
//

import UIKit

class YHPrivacyAlertView: UIView {
    typealias Block = () -> ()
    var block: Block?
    var centerView: UIView!
    var titleLabel: UILabel!
    var messageLabel: UILabel!
    var agreeButton: UIButton!
    var disAgressButton: UIButton!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.5, alpha: 0.1)
        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        tap.delegate = self
        addGestureRecognizer(tap)
        setView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setView() {
        centerView = {
            let view = UIView()
            view.backgroundColor = .white
            view.layer.cornerRadius = 12
            return view
        }()
        addSubview(centerView)
        centerView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(299)
            make.height.equalTo(187)
        }
        
        titleLabel = {
            let label = UILabel()
            label.text = "同意隐私协议"
            label.font = kFont(size: 16)
            label.textColor = UIColor(hex:0x222222)
            label.textAlignment = .center
            return label
        }()
        centerView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(19)
            make.top.equalTo(27)
        }
        
        messageLabel = {
            let label = UILabel()
            label.text = "登录注册需要您阅读并同意我们的《银河服务条款》和《银河隐私政策》"
            label.font = kFont(size: 14)
            label.textColor = UIColor(hex:0x222222)
            label.textAlignment = .left
            label.numberOfLines = 0
            return label
        }()
        centerView.addSubview(messageLabel)
        messageLabel.snp.makeConstraints { make in
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(42)
            make.top.equalTo(63)
        }
        
        disAgressButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor =  UIColor(hex:0xffffff)
            button.titleLabel?.font = kBoldFont(size: 15)
            button.contentHorizontalAlignment = .center
            button.setTitle("不同意", for: .normal)
            button.setTitleColor( UIColor(hex:0x3c86f8), for: .normal)
            button.layer.cornerRadius = 8
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor(hex: 0x3c86f8).cgColor
            button.addTarget(self, action: #selector(disagree), for: .touchUpInside)
            return button
        }()
        centerView.addSubview(disAgressButton)
        disAgressButton.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.bottom.equalTo(-21)
            make.width.equalTo(128)
            make.height.equalTo(45)
        }
        
        agreeButton = {
            let button = UIButton(type: .custom)
            button.backgroundColor =  UIColor(hex:0x3c86f8)
            button.titleLabel?.font = kBoldFont(size: 15)
            button.contentHorizontalAlignment = .center
            button.setTitle("同意", for: .normal)
            button.setTitleColor( UIColor(hex:0xffffff), for: .normal)
            button.layer.cornerRadius = 8
            button.addTarget(self, action: #selector(agree), for: .touchUpInside)
            return button
        }()
        centerView.addSubview(agreeButton)
        agreeButton.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.bottom.equalTo(-21)
            make.width.equalTo(128)
            make.height.equalTo(45)
        }
        
    }
    
    @objc func agree() {
        if let block = block {
            block()
            dismiss()
        }
    }
    
    @objc private func handleTap(_ sender: AnyObject?) {
        print("处理点击手势")
        dismiss()
    }
    
    @objc func disagree() {
        dismiss()
    }
    
    func dismiss() {
        removeFromSuperview()
    }
}

extension YHPrivacyAlertView: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer:UIGestureRecognizer, shouldReceive touch:UITouch) -> Bool {
        if touch.view == self {
            return true
        }
        return false
    }
}
