//
//  AdvisoryRootViewController+http.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/11/18.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

extension AdvisoryRootViewController {
    ///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("请查看当前网络状态！")
        }
    }
    func getScopeListHttpCmd(){
        let cmd:HttpCommand = ScopeListHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpScopeListHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        dic[kHttpSNHttpBasePostCmd_system] = "newstype"
        if AccountInfo.sharedInstance().UserId == nil || AccountInfo.sharedInstance().UserId == "" {
            dic[kHttpSNHttpBasePostCmd_companyname] = "上药控股有限公司"
        } else {
            dic[kHttpSNHttpBasePostCmd_companyname] = judgmentStrNil(str: AccountInfo.sharedInstance().infoCompanyName)
        }
        cmd.requestInfo = dic as [NSObject:AnyObject]
        let completeDelegate = SMBaseHttpComplete.init(block: block , withUserInfo: nil)
        cmd.completeDelegate=completeDelegate
        print("url==%@",cmd.getUrl())
        cmd.execute()
    }
    func httpScopeListHttpResultResponse(result:RequestResult){
        let r:ScopeListHttpResult = result as! ScopeListHttpResult
        if r.isOk() {
            httpArray = r.getScopeListHttpResult()
                as NSMutableArray
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    /// 处理数据
    func setHttpData(){
        pages = NSMutableArray()
        for index in 0..<httpArray.count {
            let model:ScopeListHttpModel = httpArray[index] as! ScopeListHttpModel
//            if (model.name != "公司信息") {
                sectionTitles.add(model.name!)
                pages.add(self.createHotPageVC(type: model.type!, title: model.name!))
//            }
        }
//        self.setSegment()
        if httpArray.count != 0 {
            initData()
//            self.tixingImage.isHidden = true
//            self.tixingLabel.isHidden = true
        } else {
//            self.tixingImage.isHidden = false
//            self.tixingLabel.isHidden = false
        }
    }
    func createHotPageVC(type:String,title:String) -> AdvisoryViewController{
        let vc:AdvisoryViewController = AdvisoryViewController.createViewController(createArgs: nil) as! AdvisoryViewController
        vc.title = title
        vc.type = type
//        vc.sequenPageType = sequenPageType
        return vc
    }
    /// 设置 segment
    func setSegment() {
        segment.sectionTitles = (sectionTitles as! [String])
        segment.indexChangeBlock = {[weak self](index:NSInteger)->() in
            let index = self?.pages.index(of: self?.pageViewController.viewControllers?.last ?? NSInteger())
            let direction:UIPageViewControllerNavigationDirection = (self?.segment.selectedSegmentIndex)! > index! ? .forward:.reverse
            self?.pageViewController.setViewControllers([(self?.selectedController())!], direction: direction, animated: true, completion: nil)
        }
    }
}
