//
//  PQPhoneLoginController.swift
//  PQSpeed
//
//  Created by SanW on 2020/6/4.
//  Copyright © 2020 BytesFlow. All rights reserved.
//

import UIKit
import BFFramework
class PQPhoneLoginController: MVBaseController {
    lazy var titleLab: UILabel = {
        let titleLab = UILabel()
        titleLab.textColor = UIColor.white
        titleLab.font = UIFont.systemFont(ofSize: 20, weight: .medium)
        titleLab.text = "手机号登录"
        titleLab.textAlignment = .center
        return titleLab
    }()

    lazy var phoneTextField: UITextField = {
        let phoneTextField = UITextField()
        phoneTextField.textColor = UIColor.black
        phoneTextField.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        let placeholder: NSMutableAttributedString = NSMutableAttributedString(string: "请输入手机号码")
        placeholder.addAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .regular), NSAttributedString.Key.foregroundColor: UIColor.hexColor(hexadecimal: "#A3A3A3")], range: NSRange(location: 0, length: placeholder.length))
        phoneTextField.attributedPlaceholder = placeholder
        phoneTextField.addTarget(self, action: #selector(valueChanged(textField:)), for: .editingChanged)
        phoneTextField.delegate = self
        phoneTextField.keyboardType = .phonePad
        phoneTextField.returnKeyType = UIReturnKeyType.next
        phoneTextField.textAlignment = .center
        phoneTextField.rightViewMode = UITextField.ViewMode.always
        return phoneTextField
    }()

    lazy var passTextField: UITextField = {
        let passTextField = UITextField()
        passTextField.textColor = UIColor.black
        passTextField.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        passTextField.isSecureTextEntry = true
        passTextField.textAlignment = .center
        let placeholder: NSMutableAttributedString = NSMutableAttributedString(string: "请输入6-20位密码")
        placeholder.addAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .regular), NSAttributedString.Key.foregroundColor: UIColor.hexColor(hexadecimal: "#A3A3A3")], range: NSRange(location: 0, length: placeholder.length))
        passTextField.attributedPlaceholder = placeholder
        passTextField.addTarget(self, action: #selector(valueChanged(textField:)), for: .editingChanged)
        passTextField.delegate = self
        passTextField.returnKeyType = UIReturnKeyType.send
        passTextField.rightViewMode = UITextField.ViewMode.always
        return passTextField
    }()

    lazy var phoneLineView: UIView = {
        let phoneLineView = UIView()
        phoneLineView.backgroundColor = UIColor.hexColor(hexadecimal: "#333333")
        return phoneLineView
    }()

    lazy var passLineView: UIView = {
        let passLineView = UIView()
        passLineView.backgroundColor = UIColor.hexColor(hexadecimal: "#333333")
        return passLineView
    }()

    lazy var phoneLoginBtn: UIButton = {
        let phoneLoginBtn = UIButton(type: .custom)
        phoneLoginBtn.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        phoneLoginBtn.backgroundColor = UIColor.black
        phoneLoginBtn.setTitleColor(UIColor.white, for: .normal)
        phoneLoginBtn.setTitle("登录", for: .normal)
        phoneLoginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        phoneLoginBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#333333")
//      phoneLoginBtn.isEnabled = false
        return phoneLoginBtn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        showNavigation()
        leftButton(image: "back_black")
        view.addSubview(titleLab)
        view.addSubview(phoneTextField)
        view.addSubview(phoneLineView)
        view.addSubview(passTextField)
        view.addSubview(passLineView)
        view.addSubview(phoneLoginBtn)
        addLayout()
    }

    func addLayout() {
        titleLab.snp.makeConstraints { make in
            make.left.right.equalTo(view)
            make.top.equalTo(view).offset(cDefaultMargin * 10)
        }
        phoneTextField.snp.makeConstraints { make in
            make.top.equalTo(titleLab.snp_bottom).offset(cDefaultMargin * 8)
            make.left.equalTo(view).offset(cDefaultMargin * 4)
            make.right.equalTo(view).offset(-cDefaultMargin * 4)
            make.height.equalTo(cDefaultMargin * 4)
        }
        phoneLineView.snp.makeConstraints { make in
            make.left.right.equalTo(phoneTextField)
            make.top.equalTo(phoneTextField.snp_bottom)
            make.height.equalTo(0.5)
        }
        passTextField.snp.makeConstraints { make in
            make.top.equalTo(phoneLineView.snp_bottom).offset(cDefaultMargin)
            make.left.right.height.equalTo(phoneTextField)
        }
        passLineView.snp.makeConstraints { make in
            make.left.right.equalTo(phoneTextField)
            make.top.equalTo(passTextField.snp_bottom)
            make.height.equalTo(0.5)
        }
        phoneLoginBtn.snp.makeConstraints { make in
            make.left.equalTo(view).offset(cDefaultMargin * 2)
            make.right.equalTo(view).offset(-cDefaultMargin * 2)
            make.height.equalTo(cDefaultMargin * 5)
            make.top.equalTo(passLineView.snp_bottom).offset(cDefaultMargin * 4)
        }
        phoneLoginBtn.addCorner(corner: 25)
    }

    @objc func loginAction() {
        if phoneTextField.text?.count ?? 0 <= 0 {
            cShowHUB(superView: view, msg: "请输入手机号码")
            return
        }
        if phoneTextField.text?.count ?? 0 > 11 {
            cShowHUB(superView: view, msg: "请输入正确的手机号码")
            return
        }
        if passTextField.text?.count ?? 0 <= 0 {
            cShowHUB(superView: view, msg: "请输入密码")
            return
        }
        if passTextField.text?.count ?? 0 < 6 {
            cShowHUB(superView: view, msg: "密码长度为6-20位")
            return
        }
        if passTextField.text?.count ?? 0 > 20 {
            cShowHUB(superView: view, msg: "密码长度为6-20位")
            return
        }
        PQMineViewModel.phoneLoginOrRegister(isLogin: true, phone: phoneTextField.text!, password: passTextField.text!) { [weak self] response, _ in
            if response == nil {
//              cShowHUB(superView: (self?.view)!, msg: msg)
                cShowHUB(superView: (self?.view)!, msg: "登录失败，请重试~")
            } else {
                cShowHUB(superView: nil, msg: "登录成功")
                self?.dismiss(animated: true, completion: {
                    postNotification(name: cLoginSuccesssNotiKey)
                })
            }
        }
    }
}

extension PQPhoneLoginController: UITextFieldDelegate {
    @objc func valueChanged(textField: UITextField) {
        if textField == phoneTextField, textField.text?.count ?? 0 > 11 {
            textField.text = String(textField.text?.prefix(11) ?? "")
        }
        if textField == passTextField, textField.text?.count ?? 0 > 20 {
            textField.text = String(textField.text?.prefix(20) ?? "")
        }
        isEnabled()
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField == phoneTextField {
            passTextField.becomeFirstResponder()
        } else {
            loginAction()
        }
        return true
    }

    override func touchesBegan(_: Set<UITouch>, with _: UIEvent?) {
        view.endEditing(true)
    }

    @objc func isEnabled() {
        let isEnabled = (phoneTextField.text?.count ?? 0 == 11 && passTextField.text?.count ?? 0 >= 6 && passTextField.text?.count ?? 0 <= 20)
//        phoneLoginBtn.isEnabled = isEnabled
        if isEnabled {
            phoneLoginBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#07C160")
        } else {
            phoneLoginBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#333333")
        }
    }
}
