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

import UIKit
//import Alamofire

extension AdvisoryViewController {
    func getAdvisoryHttpCmd(){
               
        let cmd:HttpCommand = AdvisoryHttpCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpAdvisoryHttpResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpAdvisoryHttpCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        dic[kHttpAdvisoryHttpCmd_attr] = attrDic
        dic[kHttpAdvisoryHttpCmd_orderby] = "publishtime"
        dic[kHttpAdvisoryHttpCmd_pageNum] = self.page
        dic[kHttpAdvisoryHttpCmd_pageSize] = "20"
        dic[kHttpAdvisoryHttpCmd_sort] = "desc"
        let filtersDic:NSMutableDictionary = NSMutableDictionary()
//        if sequenPageType == .CompanyNews {
            filtersDic[kHttpAdvisoryHttpCmd_value] = judgmentStrNil(str: self.type)
//        }else if sequenPageType == .CompanyAnnouncement{
//            filtersDic[kHttpAdvisoryHttpCmd_value] = "2"
//        }else if sequenPageType == .IndustryNews{
//            filtersDic[kHttpAdvisoryHttpCmd_value] = "3"
//        }
        let filtersArray = NSMutableArray()
        filtersArray.add(filtersDic)
        dic[kHttpAdvisoryHttpCmd_filters] = filtersArray
        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()
    }
    func httpAdvisoryHttpResultResponse(result:RequestResult){
        let r:AdvisoryHttpResult = result as! AdvisoryHttpResult
        if r.isOk() {
            let array:NSMutableArray = r.getAdvisoryHttpResult() as NSMutableArray
            httpListData = array[0] as! AdvisoryHttpListModelData
//            self.initSetData(httpArray: httpListData.httpModelArray)
        }else{
            let array:NSMutableArray = r.getCacheData(String(describing: self.type))
            httpListData = array[0] as! AdvisoryHttpListModelData
//            self.initSetData(httpArray: httpListData.httpModelArray)
            SMToastView.showMessage("请查看当前网络状态！")
        }
        group.leave()
    }
    func initSetData(httpArray:NSMutableArray){
        if self.page == 1 {
            self.dataSource = NSMutableArray()
            self.delegate.dataSource = self.dataSource
        }
        if httpArray.count != 1 {
            self.page = self.page+1
        }else if httpArray.count == 0 && self.page != 1 {
            SMToastView.showMessage("暂无更多数据！")
        }
        for index in 0..<httpArray.count {
            let httpModel:AdvisoryHttpListItemModel = httpArray[index] as! AdvisoryHttpListItemModel
            self.addHttpAdvisoryTableViewCellModel(httpModel: httpModel)
        }
        self.delegate.dataSource = self.dataSource
//        self.myTableView .reloadData()
    }
    func addHttpAdvisoryTableViewCellModel(httpModel:AdvisoryHttpListItemModel){
        let model = AdvisoryTableViewCellModel()
        model.className = "AdvisoryTableViewCell"
        //        model.timeStr = httpModel.createon
        model.timeStr = httpModel.publishtime
        model.titleStr = httpModel.title
        model.imageUrl = httpModel.imageUrl
        model.h = self.getCellH(httpModel: httpModel)
        model.block = {[weak self] in
            self?.openWeb(httpModel: httpModel)
        }
        self.dataSource .add(model)
    }
    func openWeb(httpModel:AdvisoryHttpListItemModel){
        let url:String = AccountInfo.sharedInstance().getAdvisoryVCUrl()+httpModel.id!
        if httpModel.value1?.count != 0 {
            self.actionGotoTitleWeb(webViewUrl: httpModel.value1!, title: self.title!, isHomePushType: .YesIsHomePushType, appid: "", isEncryptionType: .NoEncryptionType)
        }else{
            self.actionGotoTitleWeb(webViewUrl: url, title: self.title!, isHomePushType: .YesIsHomePushType, appid: "", isEncryptionType: .NoEncryptionType)
        }
        self.appViewsSstatistics(httpModel: httpModel)
    }
    func getCellH(httpModel:AdvisoryHttpListItemModel)->CGFloat{
        if httpModel.imageUrl?.count != 0 {
            return 85*HEIGHT_RATIO()
        }
        let titleLabelH = getTextHeigh(textStr: httpModel.title!, font: UIFont.test_systemFont(ofSize: 12), width: SCREEN_WHIDTH()-20*WHIDTH_RATIO())
        return titleLabelH+39
    }
    /// 应用访问量
    func appViewsSstatistics(httpModel:AdvisoryHttpListItemModel){
        let model = ClickRateModel()
        model.name = httpModel.title
        model.belong = "门户"
        model.scope = AppClickType.OftenAppType.rawValue
        model.type = "1"
        self.getClickRateHttpCmd(infoModel: model)
    }
}
