//
//  SHRecordRenameAlertView.swift
//  ShorthandMaster
//
//  Created by 明津李 on 2020/9/22.
//  Copyright © 2020 明津李. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift

class SHRecordRenameAlertView: UIView {
    
    @IBOutlet private var bgView: UIView?
    @IBOutlet private var textField: UITextField?
    @IBOutlet private var titleLab: UILabel?
    
    var title: String = "重命名" {
        didSet{
            titleLab?.text = title
        }
    }
    
    private var _placeHolder: String?
    var placeHolder: String {
        set{
            _placeHolder = newValue
            textField?.placeholder = newValue
        }
        get{
            return _placeHolder!
        }
    }
    var confirmCallBack: ((String)->Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.3)
        bgView?.layer.cornerRadius = 8
        self.textField?.becomeFirstResponder()
        self.textField?.keyboardToolbar.doneBarButton.setTarget(self, action: #selector(self.doneButtonClicked(_:)))
    }
    
    @objc @IBAction func cleanBtnClick(_ sender:UIButton){
        self.textField?.text = ""
    }
    
    @objc @IBAction func cancelBtnClick(_ sender:UIButton){
        self.removeFromSuperview()
    }
    
    @objc @IBAction func confirmBtnClick(_ sender:UIButton){
        
        if let text = textField?.text, text.length > 0{
            confirmCallBack?(textField?.text ?? "")
            self.removeFromSuperview()
        }
    }
    
    @objc func doneButtonClicked(_ sender: Any) {
        if let text = textField?.text, text.length > 0{
            confirmCallBack?(textField?.text ?? "")
            self.removeFromSuperview()
        }else{
            self.textField?.becomeFirstResponder()
        }
    }
    
    deinit {
//        IQKeyboardManager.shared.enable = false
    }
}

