//
//  CCInputTextField.swift
//  SwiftProject
//
//  Created by soldoros on 2024/1/10.
//

import UIKit

let  codeNumber:NSInteger = 6

@objc protocol CCInputTextFieldDelegate:NSObjectProtocol {
    
    //输入框输入完毕后的回调
    @objc optional func CCInputTextFieldString(string:NSString)
}


class CCInputTextField: UIView,CCTextFieldDelegate,UITextFieldDelegate {

    //代理
    weak var delegate:CCInputTextFieldDelegate?
    //输入数字的个数
    var _number:NSInteger?

    //输入框 线条 背景
    var textFields:NSMutableArray?
    var textLines:NSMutableArray?
    var backViews:NSMutableArray?
    
    //记录输入的情况
    var start:NSInteger?
    var button:UIButton?
    
    //未输入和输入后的线条颜色
    var nolColor:UIColor?
    var selColor:UIColor?
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(frame: CGRect, number:NSInteger) {
        super.init(frame: frame)
        
        _number = number
        start = 0
        _showStyle = 1
        _isEditInput = true
        _isSecurity = false
        
        textFields = NSMutableArray()
        textLines = NSMutableArray()
        backViews = NSMutableArray()
        
        nolColor = UIColor.hexStringToColor(hexString: "#264091", alpha: 0.2)
        selColor = UIColor.hexStringToColor(hexString: "#264091")
        
        for i:NSInteger in 0..._number! - 1{
            
            let width:CGFloat = self.width / CGFloat(_number!)
            
            let backView:UIView = UIView()
            backView.frame = CGRect.init(x: CGFloat(i) * width, y: 0, width: width, height: self.height)
            backView.backgroundColor = UIColor.white
            self.addSubview(backView)
            if(i > 0){
                backView.left = CGFloat(i) * (width - 0.5)
            }
            
            let textField:CCTextField = CCTextField()
            backView.addSubview(textField)
            textField.frame = backView.bounds
            textField.textAlignment = NSTextAlignment.center
            textField.tintColor = TitleColor
            textField.textColor = UIColor.black
            textField.font = UIFont.systemFont(ofSize: 25)
            textField.delegate = self
            textField.ssdeleget = self
            textField.backgroundColor = UIColor.clear
            textField.keyboardType = UIKeyboardType.numberPad
            textField.tag = 10 + i
            textField.addTarget(self, action: #selector(textFieldValueChanged(sender:)), for: UIControl.Event.editingChanged)
            
            let textLine:UIView = UIView()
            textLine.width = width - 10
            textLine.height = 1
            textLine.bottom = backView.bottom
            textLine.centerX = backView.width * 0.5
            backView.addSubview(textLine)
            textLine.backgroundColor = nolColor
            
            
            textFields?.add(textField)
            textLines?.add(textLine)
            backViews?.add(backView)
        }
        
        button = UIButton.init(type: UIButton.ButtonType.custom)
        button?.frame = self.bounds
        button?.backgroundColor = UIColor.clear
        self.addSubview(button!)
        button?.addTarget(self, action: #selector(buttonPressed(sender:)), for: UIControl.Event.touchUpInside)
    }
    
    //输入明文还是星号
    private var _isSecurity:Bool?
    var isSecurity:Bool?{
        didSet{
            _isSecurity = isSecurity
            
            for textField in textFields!{
                (textField as! CCTextField).isSecureTextEntry = _isSecurity!
            }
        }
    }
    
    //显示方框还是线条  线条1  方框2
    private var _showStyle:NSInteger?
    var showStyle:NSInteger?{
        didSet{
            _showStyle = showStyle
            
            if(_showStyle == 1){
                for i:NSInteger in 0...textFields!.count-1{
                    let textField:CCTextField = textFields![i] as! CCTextField
                    let textLine:UIView = textLines![i] as! UIView
                    
                    textField.layer.borderColor = UIColor.lightGray.cgColor
                    textField.layer.borderWidth = 0
                    textLine.isHidden = false
                }
            }
            else{
                for i:NSInteger in 0...textFields!.count-1{
                    let textField:CCTextField = textFields![i] as! CCTextField
                    let textLine:UIView = textLines![i] as! UIView
                    let backView:UIView = backViews![i] as! UIView
                    
                    textField.layer.borderColor = UIColor.lightGray.cgColor
                    textField.layer.borderWidth = 0.5
                    textLine.isHidden = true
                    backView.backgroundColor = BackGroundColor
                }
            }
        }
    }
    
    //默认点击第一个输入框
    private var _isEditInput:Bool?
    var isEditInput:Bool?{
        didSet{
            _isEditInput = isEditInput
            
            (textFields![0] as! CCTextField).becomeFirstResponder()
        }
    }
    
    
    //点击了当前视图
    @objc func buttonPressed(sender:UIButton){
        if(start! < _number! - 1){
            (textFields![start!] as! CCTextField).becomeFirstResponder()
        }
        else if(start == _number! - 1){
            (textFields![start!] as! CCTextField).becomeFirstResponder()
            if((textFields![start!] as! CCTextField).text?.count == 0){
                (textFields![start!] as! CCTextField).tintColor = TitleColor
            }
            else{
                (textFields![start!] as! CCTextField).tintColor = UIColor.clear
            }
        }
    }
    
    
    //输入操作
    @objc func textFieldValueChanged(sender:CCTextField){
        
        if(start! < _number! - 1){
            start = start! + 1
             (textFields![start!] as! CCTextField).becomeFirstResponder()
             (textLines![start! - 1] as! UIView).backgroundColor = selColor
        }
        else if(start == _number! - 1){
            if((textFields![_number! - 1] as! CCTextField).text?.count == 1){
                start = _number! - 1
                (textFields![_number! - 1] as! CCTextField).endEditing(true)
                (textLines![_number! - 1] as! UIView).backgroundColor = selColor
            }
            else{
                return
            }
        }
        
        self.returnTextFieldString()
    }
    
    //删除操作
    func CCTextKeyBordDelete() {
        
        if(start == 0){
            start = 0
        }
        else if(start! < _number! - 1){
            start = start! - 1
            (textFields![start!] as! CCTextField).text = ""
             (textFields![start!] as! CCTextField).becomeFirstResponder()
            (textLines![start!] as! UIView).backgroundColor = nolColor
        }
        else if(start == _number! - 1){
            if((textFields![_number! - 1] as! CCTextField).tintColor == UIColor.clear){
                (textFields![_number! - 1] as! CCTextField).text = ""
                (textLines![_number! - 1] as! UIView).backgroundColor = nolColor
                start = _number! - 1
                (textFields![_number! - 1] as! CCTextField).becomeFirstResponder()
                (textFields![_number! - 1] as! CCTextField).tintColor = TitleColor
            }
            else{
                start = start! - 1
                (textFields![start!] as! CCTextField).text = ""
                (textLines![start!] as! UIView).backgroundColor = nolColor
                (textFields![start!] as! CCTextField).becomeFirstResponder()
            }
        }
        
        self.returnTextFieldString()
        
    }
    
    //获取号码
    func getTextCode()->NSString{
        var string:NSString = ""
        for i:NSInteger in 0..._number! - 1{
            let textF = (textFields![i] as! CCTextField)
            string = NSString.init(format: "%@%@",string, textF.text!) as NSString
        }
        return string
    }
    
    //清空所有输入框
    func cleanAllTextF(){
        
        for i:NSInteger in 0..._number! - 1{
            (textFields![i] as! CCTextField).text = ""
            (textLines![i] as! UIView).backgroundColor = nolColor
        }
        (textFields![0] as! CCTextField).becomeFirstResponder()
    }
    
    //禁止复制粘贴
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        let menuController:UIMenuController = UIMenuController.shared
        menuController.isMenuVisible = false
        return false;
    }
    
    
    //回调
    func returnTextFieldString(){
        let string:NSString = self.getTextCode()
        delegate?.CCInputTextFieldString!(string: string)
    }
    
    override var canBecomeFirstResponder: Bool{
        return true
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        print(textField.tag)
    }
    
}
