//
//  SinglePointWeChatVC+LoginView.swift
//  MobilePortal
//
//  Created by caiyang on 2020/8/24.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

extension SinglePointWeChatVC {
    func autoLogin()  {
        if AccountInfo.sharedInstance().UserId != nil {
            //            self.loginView.passTextView.text = AccountInfo.sharedInstance().passWord
            self.loginView.userTextView.text = AccountInfo.sharedInstance().login_UaesName
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {[weak self] in
                self?.getLoginHttpPostCmdCmd()
            }
        }
    }
    func addInitLoginView(){
        loginView.isHidden = false
        loginView.passTextView.text = nil
//        loginView.weixinQQView.isHidden = true
        self.view .addSubview(loginView)
        //长按手势
        let longpressGesutre = UILongPressGestureRecognizer.init(target: self, action: #selector(longpressGesutreAction(sender:)))
        //长按时间为1秒
        longpressGesutre.minimumPressDuration = 3
        loginView.headTitleImage.addGestureRecognizer(longpressGesutre)
        loginView.headTitleImage.isUserInteractionEnabled = true
        loginView.loginButton.addTarget(self, action: #selector(onLogin), for: .touchUpInside)
        loginView.buttonAutoLogin.isHidden = true
        loginView.buttonAutoLogin.addTarget(self, action: #selector(forgetVC), for: .touchUpInside)
        loginView.userNameCountlock = {[weak self] in
            self?.loginInitType = .LoginNomType
//            self?.getLoginTypeHttpAction()
        }
//        loginView.userTextView.text = "106766"
//        loginView.passTextView.text = "123@qwert"
        
    }
    @objc func onLogin() {
        loginView.userTextView.resignFirstResponder()
        loginView.passTextView.resignFirstResponder()
        loginView.userTextView.text = loginView.userTextView.text!.replacingOccurrences(of:" ", with: "")
        loginView.passTextView.text = loginView.passTextView.text!.replacingOccurrences(of:" ", with: "")
        if loginView.userTextView.text == nil || loginView.userTextView.text?.count == 0 {
            SMToastView.showMessage("账号不能为空")
            return
        }
        if loginView.passTextView.text == nil || loginView.passTextView.text?.count == 0 {
            SMToastView.showMessage("密码不能为空")
            return
        }
        AccountInfo.sharedInstance().login_UaesName = loginView.userTextView.text
//        AccountInfo.sharedInstance().passWord = loginView.passTextView.text
        self.loginInitType = .LoginActionType
        // 获取登录方式
//        getScopeListHttpCmd()
        
        // 根据公司获取登录方式
//        self.getLoginTypeHttpAction()
        // 判断APP是否被禁用
        self.judgmentDeactivateEnable()
//        loginGetAccess_token()
    }
    /// 忘记密码
    @objc func forgetVC() {
        let vc = ForgetWebViewController()
        if AccountInfo.sharedInstance().isTest == "1" {
            vc.webViewUrl = "https://idmpre.shaphar.com/self-service/common/resetPassword"
        } else {
            vc.webViewUrl = "https://idm.shaphar.com/self-service/common/resetPassword"
        }
        vc.isHomePushType = .YesIsHomePushType
        vc.isShowTitleType = .YesShowTitleType
        vc.newType = .OrdinaryNewsType
        vc.title = ""
        self.pushViewController(viewController: vc, animated: true)
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        loginView.userTextView.resignFirstResponder()
        loginView.passTextView.resignFirstResponder()
    }
    @objc func longpressGesutreAction(sender : UILongPressGestureRecognizer){
        if sender.state == UIGestureRecognizerState.began{
            showAlert(title: "温馨提示", message: AccountInfo.sharedInstance().appUUID!, titleCancelBtn: "取消", titleSecondBtn: "复制") {[weak self] in
                self?.copyId()
            }
        }
    }
    /// 复制设备ID
    func copyId(){
        let pasteboard = UIPasteboard.general
        pasteboard.string = AccountInfo.sharedInstance().appUUID
        SMToastView.showMessage("内容已复制！")
    }
}
