//
//  MineViewController+http.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/15.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

extension MineViewController {
    
    // MARK:登录接口
    func getLoginHttpPostCmdCmd(){
        showHintView()
        let cmd:HttpCommand = LoginHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpLoginHttpPostResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpLoginHttpPostCmd_userid] = self.loginView.userTextView.text
        dic[kHttpLoginHttpPostCmd_password] = self.loginView.passTextView.text
        //dic[kHttpLoginHttpPostCmd_password] = ""
        dic[kHttpLoginHttpPostCmd_ticket] = AccountInfo.sharedInstance().appUUID
        dic[kHttpLoginHttpPostCmd_type] = ""
        
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpLoginHttpPostCmd_attr] = dic
        
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = attrDic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpLoginHttpPostResultResponse(result:RequestResult){
         hidenHintView()
        let r:LoginHttpPostResult = result as! LoginHttpPostResult
        if r.isOk() {
            let array:NSMutableArray = r.getLoginHttpPostList()
            if array.count != 0{
                SMToastView.showMessage(array[0] as? String )
            }else{
                if loginView.passTextView.text == "123456"{
                    //AccountInfo.sharedInstance().clearAccount()
                    SMToastView.showMessage("您当前的密码为初始密码。请修改密码！")
                    self.gotoChangePassWordViewController(changePassWordType: .LoginChangePassWordType)
                }else{
                    getDeviceInformationHttpCmd()
                }
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        
    }
    // MARK:上传手机信息成功后 刷新页面
    override func httpDeviceInformationHttpResultResponse(result: RequestResult) {
        let r:DeviceInformationHttpResult = result as! DeviceInformationHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getDeviceInformationHttpResult()
            if array.count != 0{
                SMToastView.showMessage((array[0] as! String) )
            }else{
                removeView()
                notLoginAction()
                NotificationCenter.default.post(name: Notification.Name(rawValue: "irregularViewLogin"), object: "0")
//                JPUSHService.setTags([AccountInfo.sharedInstance().appUUID!], alias: AccountInfo.sharedInstance().UserId) { (code, tags, alias) in
//                    print(code,tags!,alias!)
//                }
                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)
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
    // MARK:获取个人信息接口
    func getMyInformationHttpGetCmd(){
        let cmd:HttpCommand = MyInformationHttpGetCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpMyInformationHttpGetResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpMyInformationHttpGetCmd_userid] = AccountInfo.sharedInstance().UserId
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = (dic as! [AnyHashable : Any])
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    func httpMyInformationHttpGetResultResponse(result:RequestResult){
        let r:MyInformationHttpGetResult = result as! MyInformationHttpGetResult
        if r.isOk() {
            let array:NSMutableArray = r.getMyInformationAllData()
            self.inforHttpModel = MyInformationHttpModel()
            self.inforHttpModel = array[0] as! MyInformationHttpModel
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        headArray = NSMutableArray()
        self.addHeadImageModel(httpInfoModel: inforHttpModel)
        group.leave()
    }
    // MARK:获取我的应用接口
    func getMyApplicationsHttpCmd(){
        let cmd:HttpCommand = MyApplicationsHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpMyApplicationsHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        
        dic[kHttpMyApplicationsHttpCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpMyApplicationsHttpCmd_type] = "10"
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    func httpMyApplicationsHttpResultResponse(result:RequestResult){
        let r:MyApplicationsHttpResult = result as! MyApplicationsHttpResult
        var myApplicationsModel = MyApplicationsHttpModel()
//        self.myClassArray = NSMutableArray()
        if r.isOk() {
            let array:NSMutableArray = r.getMyApplicationsHttpResult()
            myApplicationsModel = array[0] as! MyApplicationsHttpModel
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        self.myClassArray = NSMutableArray()
        self.addMyApplicationsHttpCellData(httpModel: myApplicationsModel,type:"10", applicationsArray: self.myClassArray)
        group.leave()
    }
    // MARK:获取我的常接口
    func getMyApplicationsHttpCmdTwo(){
        let cmd:HttpCommand = MyApplicationsHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpMyApplicationsHttpResultResponseTwo(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        
        dic[kHttpMyApplicationsHttpCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpMyApplicationsHttpCmd_type] = "11"
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    func httpMyApplicationsHttpResultResponseTwo(result:RequestResult){
        let r:MyApplicationsHttpResult = result as! MyApplicationsHttpResult
        var httpModel = MyApplicationsHttpModel()
        if r.isOk() {
            let array:NSMutableArray = r.getMyApplicationsHttpResult()
            httpModel = array[0] as! MyApplicationsHttpModel
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        self.oftenClassArray = NSMutableArray()
        self.addMyApplicationsHttpCellData(httpModel: httpModel,type:"11", applicationsArray: self.oftenClassArray)
        group.leave()
    }
    //添加分类
    func addMyApplicationsHttpCellData(httpModel:MyApplicationsHttpModel,type:String?,applicationsArray:NSMutableArray)  {
        
        let homeTitleModel = MineTitleTableViewCellModel()
        homeTitleModel.className = "MineTitleTableViewCell"
        if type == "10" {
            // 我的应用
            homeTitleModel.titleValue = "lineUrl"
            homeTitleModel.nameImage = "myApplications_image"
        }else{
            // 我的常用
            homeTitleModel.titleValue = "lineUrl"
            homeTitleModel.nameImage = "myUsual_image"
        }
        homeTitleModel.isShow = false
        let detailedModel = MineDetailedTableViewCellModel()
        detailedModel.className = "MineDetailedTableViewCell"
        detailedModel.cellNum = self.cellNum
        //添加详细分类
        for index in 0..<httpModel.listArray.count{
            let httpDetailModel:AdvisoryHttpListModel = httpModel.listArray[index] as! AdvisoryHttpListModel
            let detailModel = MineCollectionViewCellModel()
            detailModel.className = "MineCollectionViewCell"
            detailModel.titleValue = httpDetailModel.title
            detailModel.imageName = httpDetailModel.imageUrl
            detailModel.biaojiNum = httpDetailModel.value2
            detailModel.openUrl = httpDetailModel.openurl
            detailModel.comments = httpDetailModel.comments
            if type == "10" {
                // 我的应用
                detailModel.applicationType = .MyApplicationType
            }else{
                // 我的常用
                detailModel.applicationType = .OftenApplicationType
            }
            detailModel.isInstallation = self.isInstallation(urlString: httpDetailModel.openurl!)
            detailModel.httpDetailModel = httpDetailModel
            detailModel.block = { [weak self](value:Any) in
//                self?.gotoWeb(httpDetailModel: httpDetailModel, type: type!)
                self?.classAction(selecHttpModel:httpDetailModel , selecCellModel: detailModel)
            }
            detailedModel.detaileArray .add(detailModel)
        }
        applicationsArray .add(homeTitleModel)
        addCellModel(array: detailedModel.detaileArray, str: type!, cellModelArray: httpModel.listArray)
        applicationsArray .add(detailedModel)
    }
    //添加更多按钮
    func addCellModel(array:NSMutableArray,str:String,cellModelArray:NSMutableArray){
        let detailModel = MineCollectionViewCellModel()
        detailModel.className = "MineCollectionViewCell"
        detailModel.titleValue = "更多"
        detailModel.imageName = "more_image"
        detailModel.comments = "123"
        detailModel.applicationType = .OftenApplicationType
        detailModel.block = { [weak self](value:Any) in
            self?.gotoMineMore(str: str, cellModelArray: cellModelArray)
        }
        array .add(detailModel)
    }
    //跳转更多页面
    func gotoMineMore(str:String,cellModelArray:NSMutableArray){
        let vc:MineMoreViewController=MineMoreViewController()
//        vc.mineCellModelArray = cellModelArray
        if str == "10" {
            vc.applicationType = .MyApplicationType
            vc.title = "我的应用"
        }else{
            vc.applicationType = .OftenApplicationType
            vc.title = "我的常用"
        }
        vc.hidesBottomBarWhenPushed = true
        self.pushViewController(viewController: vc, animated: true)
    }
    
}
