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

import UIKit

extension MineViewController {
    //MARK：获取对应服务
    func getApplicationByBelongAction(applicationName:String)  {
        showHintView()
        let cmd:HttpCommand = ApplicationByBelongHttpGetCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpApplicationByBelongHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpApplicationByBelongHttpGetCmd_applicationName] = applicationName
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    @objc func httpApplicationByBelongHttpResultResponse(result:RequestResult){
        let r:ApplicationByBelongHttpResult = result as! ApplicationByBelongHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getApplicationByBelongHttpResult()
            if array.count != 0{
                let httpDetailModel:ApplicationHttpListModel = array[0] as! ApplicationHttpListModel
                self.refreshTokenHttpAction(selecHttpModel: httpDetailModel)
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        self.hidenHintView()
    }
    //MARK：获取消息详情
    func getApplicationDetailsHttpActin(applicationName:String)  {
        let cmd:HttpCommand = ApplicationDetailsHttpGetCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpApplicationDetailsHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpApplicationDetailsHttpGetCmd_applicationName] = applicationName
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    @objc func httpApplicationDetailsHttpResultResponse(result:RequestResult){
        let r:ApplicationDetailsHttpResult = result as! ApplicationDetailsHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getApplicationDetailsHttpResult()
            if array.count != 0{
                let httpDetailModel:ApplicationHttpListModel = array[0] as! ApplicationHttpListModel
                self.refreshTokenHttpAction(selecHttpModel: httpDetailModel)
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
    }
    // MARK:消息数量
    func getMessageNunberShowHttpPostCmd(){
        let cmd:HttpCommand = MessageNunberShowHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpMessageNumberShowHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        var filtersArr = NSMutableArray()
        let dic1:[String:Any] = ["value":judgmentStrNil(str: AccountInfo.sharedInstance().UserId!),"key":"targetid"]
        let dic2:[String:Any] = ["value":"1","key":"state"]
        let dic3:[String:Any] = ["value":"0","key":"status"]
        filtersArr = [dic1,dic2,dic3]
        dic[kHttpMessageNunberShowHttpPostCmd_pageNum] = "1"
        dic[kHttpMessageNunberShowHttpPostCmd_pageSize] = "10000"
        dic[kHttpMessageNunberShowHttpPostCmd_orderby] = "create_time"
        dic[kHttpMessageNunberShowHttpPostCmd_sort] = "desc"
        dic[kHttpMessageNunberShowHttpPostCmd_filters] = filtersArr
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    @objc func httpMessageNumberShowHttpResultResponse(result:RequestResult){
        let r:MessageNumberShowHttpResult = result as! MessageNumberShowHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getMessageNumberShowHttpResult()
            if array.count != 0{
                let model:MessageNumberShowModel = array[0] as! MessageNumberShowModel
                AccountInfo.sharedInstance().messageNumber = model.total
            }
        }else{
            //            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    // MARK:待办已办总数
    func getUpcomingCountHttpPostCmd(){
        /// 获取d统一待办 总数目
        UpcomingNewCountHttpAction.sharedInstance().getAllApplicationHttpListCmd {[weak self] in
            self?.tabBarController?.tabBar.showBadge(onItemIndex: 1, angNum: numBigData(numStr: judgmentStrNil(str: AccountInfo.sharedInstance().upcomingCount))
            )
        }
        group.leave()
        return
        let cmd:HttpCommand = UpcomingCountHttpPostCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpUpcomingCountHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpUpcomingCountHttpPostCmd_targetid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpUpcomingCountHttpPostCmd_empuid] = judgmentStrNil(str:AccountInfo.sharedInstance().sysUserId)
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.requestInfo = dic as? [AnyHashable : Any]
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    @objc func httpUpcomingCountHttpResultResponse(result:RequestResult){
        let r:UpcomingCountHttpResult = result as! UpcomingCountHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getUpcomingCountHttpResult()
            if array.count != 0{
                let model:UpcomingCountModel = array[0] as! UpcomingCountModel
                AccountInfo.sharedInstance().upcomingCount = model.ytotal
                AccountInfo.sharedInstance().doneCount = model.dtotal
                self.tabBarController?.tabBar.showBadge(onItemIndex: 1, angNum: judgmentStrNil(str: AccountInfo.sharedInstance().doneCount))
            }
        }else{
//            SMToastView.showMessage("请查看当前网络状态！")
        }
        
    }
    
    // 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().loginType != "1"{
                    //AccountInfo.sharedInstance().clearAccount()
                    SMToastView.showMessage("您当前的密码为初始密码。请修改密码！")
                    self.gotoChangePassWordViewController(changePassWordType: .OrdinaryChangePassWordType)
                }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")
//                NotificationCenter.default.post(name: Notification.Name(rawValue: "setVoiceViewWindow"), 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
                }
            }
        }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()
        let cmd:HttpCommand = UserInfoHttpPostCmd.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()
        let arrtDic:NSMutableDictionary = NSMutableDictionary()
        arrtDic[kHttpUserInfoHttpPostCmd_name] = AccountInfo.sharedInstance().UserId
        dic[kHttpUserInfoHttpPostCmd_attr] = arrtDic
        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("请查看当前网络状态！")
//        }
        let r:UserInfoHttpPostResult = result as! UserInfoHttpPostResult
        if r.isOk() {
            let array:NSMutableArray = r.getUserInfoHttpPostResult()
            self.inforHttpModel = MyInformationHttpModel()
            self.inforHttpModel = array[0] as! MyInformationHttpModel
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        headArray = NSMutableArray()
        self.addHeadImageModel(httpInfoModel: inforHttpModel)
        group.leave()
    }
    // MARK:获取我的自定义应用
    func getMyApplicationListHttpCmd(){
        let cmd:HttpCommand = MyApplicationListHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpMyApplicationListHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpMyApplicationListHttpCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpMyApplicationListHttpCmd_system] = "0"
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
        
    }
    func httpMyApplicationListHttpResultResponse(result:RequestResult){
        let r:MyApplicationListHttpResult = result as! MyApplicationListHttpResult
        var array = NSMutableArray()
        if r.isOk() {
            array = r.getMyApplicationListHttpResult()
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        self.oftenClassArray = NSMutableArray()
        self.setScopeList(scopeHttpArray: array, appModelArray: self.oftenClassArray)
        group.leave()
    }
    /// 设置服务范围数组
    func setScopeList(scopeHttpArray:NSMutableArray,appModelArray:NSMutableArray){
        for index in 0..<scopeHttpArray.count {
            let scopeHttpModel:ApplicationHttpScopListModel = scopeHttpArray[index] as! ApplicationHttpScopListModel
            let titleModel = MineTitleTableViewCellModel()
            titleModel.className = "MineTitleTableViewCell"
            titleModel.titleValue = scopeHttpModel.scope
            appModelArray.add(titleModel)
            if scopeHttpModel.scopes == "3" {
                for i in 0..<scopeHttpModel.application.count{
                    let webHttpModel:ApplicationHttpListModel=scopeHttpModel.application[i] as! ApplicationHttpListModel
                    let webModel = MineWebTableViewCellModel()
                    webModel.className = "MineWebTableViewCell"
                    webModel.webUrl = webHttpModel.hurl
                    webModel.title = webHttpModel.name
                    webModel.hightype = webHttpModel.hightype
                    webModel.webHeigh = self.getWebCellHeight(type: webHttpModel.hightype!)
                    appModelArray.add(webModel)
                }
                scopeHttpModel.application = []
            }
            let detailedModel = MineDetailedTableViewCellModel()
            detailedModel.className = "MineDetailedTableViewCell"
            detailedModel.cellNum = self.cellNum
            detailedModel.lineViewShowType = true
            detailedModel.titleValue = scopeHttpModel.scope
            self.addMyApplicationsHttpCellData(detailedModelArray: detailedModel.detaileArray, applicationsHttpArray: scopeHttpModel.application, scopeHttpModel: scopeHttpModel)
            appModelArray.add(detailedModel)
        }
    }
    func getWebCellHeight(type:String) -> CGFloat {
        var height = 100*HEIGHT_RATIO()
        if type == "2" {
            height = 150*HEIGHT_RATIO()
        } else if type == "3" {
            height = 200*HEIGHT_RATIO()
        } else {
            height = 100*HEIGHT_RATIO()
        }
        return CGFloat(height)
    }
    //添加分类
    func addMyApplicationsHttpCellData(detailedModelArray:NSMutableArray,applicationsHttpArray:NSMutableArray,scopeHttpModel:ApplicationHttpScopListModel)  {
        //添加详细分类
        for index in 0..<applicationsHttpArray.count{
            let httpDetailModel:ApplicationHttpListModel = applicationsHttpArray[index] as! ApplicationHttpListModel
            let detailModel = MineCollectionViewCellModel()
            detailModel.className = "MineCollectionViewCell"
            detailModel.titleValue = httpDetailModel.name
            detailModel.imageName = httpDetailModel.imageUrl
            detailModel.biaojiNum = httpDetailModel.value2
            detailModel.openUrl = httpDetailModel.iosopenurl
            detailModel.platforms = httpDetailModel.platforms
            detailModel.isInstallation = self.isInstallation(urlString: httpDetailModel.iosopenurl!)
            detailModel.block = { [weak self](value:Any) in
//                self?.gotoWeb(httpDetailModel: httpDetailModel, type: type!)
//                self?.classAction(selecHttpModel:httpDetailModel)
                self?.refreshTokenHttpAction(selecHttpModel: httpDetailModel)
            }
            detailedModelArray .add(detailModel)
        }
        if scopeHttpModel.scopes != "9" && scopeHttpModel.scopes != "12" {
           addCellModel(array: detailedModelArray , scopeHttpModel: scopeHttpModel)
        }
        
    }
    ///添加更多按钮
    func addCellModel(array:NSMutableArray ,scopeHttpModel:ApplicationHttpScopListModel){
        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(title: scopeHttpModel.scope!, str: scopeHttpModel.scopes!)
        }
        array .add(detailModel)
    }
    //跳转更多页面
    func gotoMineMore(title:String,str:String){
        let vc:MineMoreViewController=MineMoreViewController()
        vc.title = title
        vc.scope = str
        vc.scopes = title
        vc.systemType = .MineSystemType
        vc.hidesBottomBarWhenPushed = true
        self.pushViewController(viewController: vc, animated: true)
    }
    
}
