//
//  YHCollegeSearchBar.swift
//  galaxy
//
//  Created by edy on 2024/2/22.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHCollegeSearchBar: UIView, UITextFieldDelegate {

    static let height = 40.0
    static let confirmBtnHeight = height-8.0
    
    var textChange:((String?)->Void)?
    var confirmBlock:(()->Void)?
    var placeHolder:String? {
        didSet {
            if let placeholder = placeHolder {
                textField.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [NSAttributedString.Key.foregroundColor : UIColor.placeHolderColor])
            }
        }
    }
    private lazy var contentView = {
        let view = UIView()
        view.backgroundColor = .contentBkgColor
        view.clipsToBounds = true
        return view
    }()
    
    lazy var textField:UITextField = {
        let textField = UITextField()
        textField.backgroundColor = .clear
        textField.attributedPlaceholder = NSAttributedString(string: "请输入学校全称", attributes: [NSAttributedString.Key.foregroundColor : UIColor.placeHolderColor])
        textField.font = UIFont.PFSC_M(ofSize: 14)
        textField.tintColor = UIColor.brandMainColor
        textField.textColor = UIColor.mainTextColor
        textField.delegate = self
        return textField
    }()
    
    lazy var confirmBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("确认".local, for: .normal)
        btn.titleLabel?.font = UIFont.PFSC_M(ofSize: 14)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = UIColor.brandMainColor
        btn.addTarget(self, action: #selector(confirmBtnClicked), for: .touchUpInside)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    static func createBar() -> YHCollegeSearchBar {
        let view = YHCollegeSearchBar(frame: CGRect(x: 0, y: 0, width: KScreenWidth-32, height: height))
        return view
    }
    
    @objc func confirmBtnClicked() {
        if let confirmBlock = confirmBlock {
            confirmBlock()
        }
    }
    
    func createUI() {
        
        self.backgroundColor = .white
        
        contentView.addSubview(textField)
        contentView.addSubview(confirmBtn)
        self.addSubview(contentView)
        
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        textField.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(Self.height/2.0)
            make.right.equalTo(confirmBtn.snp.left).offset(-4)
            make.centerY.equalToSuperview()
        }
        
        confirmBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 66, height: Self.height-8.0))
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-4)
        }
    }

    func textFieldDidChangeSelection(_ textField: UITextField) {
        let text = textField.text ?? ""
        textChange?(textField.text)
    
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string.count > 0 {
            let newText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
            if newText.count > 100 {
                return false
            }
        }
        return true
    }
}
