//
//  LoginViewController.swift
//  MobilePortal
//
//  Created by caiyang on 2020/3/24.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

class LoginViewController: BaseViewController,WXAuthDelegate,WXApiDelegate,UIAlertViewDelegate {

    lazy var loginView : LoginView = {
        let loginView = LoginView.init(frame: self.view.frame)
        loginView.weixinQQView.weixinBtn.addTarget(self, action: #selector(sendAuthRequest), for: .touchUpInside)
        return loginView;
    }()
    var httpViewModel = WeChatViewModel()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.wr_setNavBarBackgroundAlpha(0)
        if #available(iOS 13.0, *) {
            if SCREEN_HEIGHT() >= 812 {
                self.wr_setNavBarShadowImageHidden(true)
            }
        } else {
            self.wr_setNavBarShadowImageHidden(true)
        }
        //        addNotificationCenter()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = loginBg_Color
        addInitLoginView()
        getNewVersionUpdateHttpCmd()
        // Do any additional setup after loading the view.
    }
    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
        self.view .addSubview(loginView)
        loginView.loginButton.addTarget(self, action: #selector(onLogin), for: .touchUpInside)
    }
    @objc func onLogin() {
        loginView.userTextView.resignFirstResponder()
        loginView.passTextView.resignFirstResponder()
        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
        getLoginHttpPostCmdCmd()
        
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        loginView.userTextView.resignFirstResponder()
        loginView.passTextView.resignFirstResponder()
    }
    func newHttpAction(){
//        showHintView()
//        queue.async(group: group, execute: {[weak self] in
//            self?.group.enter()
//            self?.getLoginHttpPostCmdCmd()
//        })
//        group.notify(queue: DispatchQueue.main) {[weak self] in
            NotificationCenter.default.post(name: Notification.Name(rawValue: "addTabBarWindow"), object: "0")
            JPUSHService.setTags([AccountInfo.sharedInstance().appUUID!], completion: { (iResCode, iTags, seq) in
                print(iResCode,iTags!,seq)
                } as JPUSHTagsOperationCompletion, seq: 0)
            JPUSHService.setAlias(AccountInfo.sharedInstance().UserId, completion: { (iResCode, iTags, seq) in
                print(iResCode,iTags!,seq)
            }, seq: 1)
        SinglePointWeChatHttp.sharedInstance().juphRegistrationIDActionHttp { [weak self] (value:Any) in
        } errorBlock: { (value:Any) in
        }
//            self?.hidenHintView()
//        }
    }
    ///加载中。。。
    override func showHintView(){
        CBToast.showIndicatorToastAction(message: "加载中...")
    }
    override func hidenHintView(){
        CBToast.hiddenIndicatorToastAction()
    }
}
