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

import UIKit

class YHPhoneMessageView: UIView {
    typealias PhoneBlock = (_ count: Int) -> ()
    typealias Block = () -> ()
    var block: Block?
    var phoneBlock: PhoneBlock?
    var messageButton: UIButton!
    var phoneTextField: UITextField!
    override init(frame: CGRect) {
        super.init(frame: frame)
        setView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setView() {
        messageButton = {
            let button = UIButton(type: .custom)
            button.titleLabel?.font = UIFont(name: "DINAlternate-Bold", size: 18)
            button.setTitle("+86", for: .normal)
            button.setTitleColor(UIColor.mainTextColor, for: .normal)
            button.addTarget(self, action: #selector(messageClick), for: .touchUpInside)
            button.titleLabel?.textAlignment = .left
            return button
        }()
        addSubview(messageButton)
        messageButton.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.width.equalTo(30)
            make.height.equalTo(20)
        }
        
        phoneTextField = {
            let text = UITextField()
            text.placeholder = "请输入您的手机号"
            text.font = UIFont(name: "DINAlternate-Bold", size: 18)
            text.textColor = UIColor.mainTextColor
            text.textAlignment = .left
            text.keyboardType = .phonePad
            text.clearButtonMode = .always
            text.delegate = self
            text.addTarget(self, action: #selector(textFeildChange), for: .allEvents)
            return text
        }()
        addSubview(phoneTextField)
        phoneTextField.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(messageButton.snp.right).offset(16)
            make.height.equalTo(20)
            make.right.equalToSuperview()
        }
        
        
        let line = UIView()
        line.backgroundColor = UIColor(hex: 0xf0f3f7, alpha: 0.8)
        addSubview(line)
        line.snp.makeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    @objc func messageClick() {
        if let block = block {
            block()
        }
    }
    
    @objc func textFeildChange() {
        if let block = phoneBlock {
            block(phoneTextField.text?.count ?? 0)
        }
    }
}

extension YHPhoneMessageView: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let newText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        if newText.count > 11 {
            return false
        }
        return true
    }
}
