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

import UIKit

extension MineViewController {
    //点击应用响应事件
    func classAction(selecHttpModel:AdvisoryHttpListModel,selecCellModel:MineCollectionViewCellModel){
        self.selecClassModel = selecHttpModel
        if selecCellModel.applicationType == .MyApplicationType {
            myApplicationAction(httpDetailModel: selecHttpModel)
        } else {
            oftenApplicationType(httpDetailModel: selecHttpModel)
        }
        
    }
    //我的应用响应事件
    private func myApplicationAction(httpDetailModel:AdvisoryHttpListModel){
        if httpDetailModel.comments != nil && httpDetailModel.comments?.count != 0{
            let url:String = httpDetailModel.comments!+"?docId="+judgmentStrNil(str: httpDetailModel.id)+"&userId="+AccountInfo.sharedInstance().UserId!+"&userName="+judgmentStrNil(str: AccountInfo.sharedInstance().userName)
            self.actionGotoTitleWeb(webViewUrl: url, title: httpDetailModel.title!, isHomePushType: .YesIsHomePushType)
            return
        }
        if self.isInstallation(urlString: httpDetailModel.openurl!) == false{
            //打开APP
            self.downloadOpenApp(urlString: httpDetailModel.openurl!, openUrlType: .OpenAppType)
        }else{
            if httpDetailModel.appid?.count == 0{
                SMToastView.showMessage("该应用暂无下载地址，请联系管理员！")
                return
            }
            self.downloadOpenApp(urlString: httpDetailModel.appid!, openUrlType: .DownloadType)
        }
    }
    //我的常用响应事件
    func oftenApplicationType(httpDetailModel:AdvisoryHttpListModel){
        let companyInfo:SelecCompanyDepartmentInfo = getSelecCompanyDepartment(tags: httpDetailModel.tags ?? String())
        if companyInfo.tags == nil {
            getSelectCompanyHttpCmd()
        }else{
            if httpDetailModel.tags == "ERP" && companyInfo.sysOrg == ""{
                SMToastView.showMessage("ERP组织获取失败！")
                return
            }
            self.setCacheAccountInfo(companyInfo: companyInfo, selecClassModel: self.selecClassModel)
            return
        }
    }
    // MARK:获取公司列表
    private func getSelectCompanyHttpCmd(){
        showHintView()
        let cmd:HttpCommand = SelectCompanyHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpSelectCompanyHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpSelectCompanyHttpCmd_companyname] = ""
        attrDic[kHttpSelectCompanyHttpCmd_sysname] = self.selecClassModel.tags
        attrDic[kHttpSelectCompanyHttpCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpSelectCompanyHttpCmd_attr] = attrDic
        dic[kHttpSelectCompanyHttpCmd_dbName] = ""
        dic[kHttpSelectCompanyHttpCmd_length] = "100"
        dic[kHttpSelectCompanyHttpCmd_start] = "1"
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    private func httpSelectCompanyHttpResultResponse(result:RequestResult){
        let r:SelectCompanyHttpResult = result as! SelectCompanyHttpResult
        if r.isOk() {
            let array = r.getSelectCompanyHttpResult()
            let selectCompanyModel:SelectCompanyHttpModel = array[0] as! SelectCompanyHttpModel
            companyListModelArray = selectCompanyModel.sysCompany.data
            departmentListModelArray = selectCompanyModel.orgList.data
            if companyListModelArray.count == 0{
                //SMToastView.showMessage("暂无公司！")
                if (self.selecClassModel.tags == "ERP" || self.selecClassModel.tags == "erp") {
                    SMToastView.showMessage("ERP部门组织获取失败！")
                    return
                }
                self.setWebUrlAction(selecClassModel: self.selecClassModel)
            }else if companyListModelArray.count == 1 && departmentListModelArray.count == 0  {
                if (self.selecClassModel.tags == "ERP" || self.selecClassModel.tags == "erp") {
                    SMToastView.showMessage("ERP部门组织获取失败！")
                    return
                }
                //SMToastView.showMessage("暂无部门！")
                self.selectOneCompany(companyListArray: companyListModelArray)
                //setUrl()
            }else if companyListModelArray.count == 1 && departmentListModelArray.count == 1  {
                self.selectCompanyDeparttment(companyListArray: companyListModelArray, departmentListArray: departmentListModelArray)
            }else if companyListModelArray.count != 1 || departmentListModelArray.count != 1  {
                gotoCompanyListVC()
            }
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        hidenHintView()
    }
    //解析出只有一个公司和只有一个部门时
    func selectCompanyDeparttment(companyListArray:NSMutableArray,departmentListArray:NSMutableArray){
        let companyModel:SelectCompanyHttpCompanyDataModel = companyListModelArray[0] as! SelectCompanyHttpCompanyDataModel
        let departmentModel:SelectCompanyHttpOrgListDataModel = departmentListArray[0] as! SelectCompanyHttpOrgListDataModel
        self.setClassWebUrlAccountInfo(selecClassModel: self.selecClassModel, companyModel: companyModel, departmentModel: departmentModel)
    }
    //解析出只有一个公司时
    func selectOneCompany(companyListArray:NSMutableArray){
        let companyModel:SelectCompanyHttpCompanyDataModel = companyListModelArray[0] as! SelectCompanyHttpCompanyDataModel
        self.setClassWebUrlAccountInfo(selecClassModel: self.selecClassModel, companyModel: companyModel, departmentModel: SelectCompanyHttpOrgListDataModel())
        
    }
    func gotoCompanyListVC()  {
        let vc:SelectCompanyViewController=SelectCompanyViewController.createViewController(createArgs: nil) as! SelectCompanyViewController
        vc.selecClassModel = self.selecClassModel
        vc.block = {[weak self] (value:Any) in
            self?.mineGotoWebVC()
        }
        vc.hidesBottomBarWhenPushed = true
        self.pushViewController(viewController: vc, animated: true)
    }
    
    @objc func mineGotoWebVC(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
           self.setWebUrlAction(selecClassModel: self.selecClassModel ?? AdvisoryHttpListModel())
        }
    }
}
