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

import UIKit

extension HomeViewController {
    func getHttpHomeClassListCmd(){
        let cmd:HttpCommand = HttpHomeClassListCmd.init(version: PHttpVersion_v1)
        let block:httpBlock = {[weak self] (result:RequestResult!,useInfo:Any!)->() in
            if result != nil {
                self?.httpHttpHomeClassListResultResponse(result: result)
            }else{
                print("空的")
            }
        }
        let dic:NSMutableDictionary = NSMutableDictionary()
        let attrDic:NSMutableDictionary = NSMutableDictionary()
        attrDic[kHttpHttpHomeClassListCmd_empid] = judgmentStrNil(str: AccountInfo.sharedInstance().UserId)
        
        dic[kHttpHttpHomeClassListCmd_attr] = attrDic
        
        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 httpHttpHomeClassListResultResponse(result:RequestResult){
        let r:HttpHomeClassListResult = result as! HttpHomeClassListResult
        if r.isOk() {
            let array:NSMutableArray = r.getTheAllData()
            httpInfoModel = array.lastObject as! HomeHttpInfoModel
        }else{
            SMToastView.showMessage("请查看当前网络状态！")
//            let array:NSMutableArray = r.getCacheData()
//            httpInfoModel = array.lastObject as! HomeHttpInfoModel
        }
        self.dataSource = NSMutableArray()
        self.setResolveHttpModel(httpModel: httpInfoModel)
//        self.delegate.dataSource = self.dataSource
        group.leave()

    }
    func setResolveHttpModel(httpModel:HomeHttpInfoModel){
        self.addCarouselTableViewCell(bannerModelArray: httpModel.banner)
        self.addHttpCellData(menuModel: httpModel)
        self.addHeadlinesTitleMore(topmore: httpModel.topmore, toptitle: httpModel.toptitle)
        self.addHeadlinesMore(topnews: httpModel.topnews)
    }
    
    //添加轮播图
    func addCarouselTableViewCell(bannerModelArray:NSMutableArray){
        bannerArray = NSMutableArray()
        carouseCellModel = HomeCarouselTableViewCellModel()
        carouseCellModel.className = "HomeCarouselTableViewCell"
        for index in 0..<bannerModelArray.count {
            let bannerModel:HomeHttpInfoModelBannerModel = bannerModelArray[index] as! HomeHttpInfoModelBannerModel
            carouseCellModel.imageUrlArray .add(bannerModel.image ?? String())
            carouseCellModel.pageUrlArray.add(bannerModel.comments ?? String())
            carouseCellModel.titleUrlArray.add(bannerModel.title ?? String())
        }
        //print(carouseCellModel.imageUrlArray.count)
        carouseCellModel.block = {[weak self] (sender: Any) in
//            self?.actionGotoWeb(webViewUrl: sender as! String, isHomePushType: .YesIsHomePushType)
        }
        bannerArray.add(carouseCellModel)
        self.dataSource .add(bannerArray)
    }
    
    func addHttpCellData(menuModel:HomeHttpInfoModel)  {
        classArray = NSMutableArray()
        detailedModel.className = "MineDetailedTableViewCell"
        detailedModel.cellNum = self.cellNum
        detailedModel.detaileArray = NSMutableArray()
        for index in 0..<menuModel.menu.count {
            let httpDetailModel:HomeHttpInfoModelMenuModel = menuModel.menu[index] as! HomeHttpInfoModelMenuModel
            let detailModel = MineCollectionViewCellModel()
            detailModel.className = "MineCollectionViewCell"
            detailModel.titleValue = httpDetailModel.title
            detailModel.imageName = httpDetailModel.image
            detailModel.comments = httpDetailModel.comments
            detailModel.block = { [weak self](value:Any) in
                //self?.gotoWeb(model: httpDetailModel)
                self?.actionGotoWeb(webViewUrl: value as! String, isHomePushType: .YesIsHomePushType)
            }
            detailedModel.detaileArray .add(detailModel)
        }
        self.addCellModel(array: detailedModel.detaileArray)
        classArray .add(detailedModel)
        self.dataSource .add(classArray)
    }
    //上药应用更多
    func addCellModel(array:NSMutableArray){
        detailModel.className = "MineCollectionViewCell"
        detailModel.titleValue = "更多"
        detailModel.imageName = "more_image"
        detailModel.comments = "123"
        detailModel.block = { [weak self](value:Any) in
            self?.gotoHomeMore()
        }
        array .add(detailModel)
    }
    //跳转更多
    func gotoHomeMore(){
        let vc:HomeMoreViewController = HomeMoreViewController()
        vc.hidesBottomBarWhenPushed = true
        self.pushViewController(viewController: vc, animated: true)
    }
    
    //添加上药头条
    func addHeadlinesTitleMore(topmore:NSMutableArray,toptitle:NSMutableArray){
        headlinesTitleArray = NSMutableArray()
        headlinesMoreCellModel.className = "HeadlinesMoreTableViewCell"
        for index in 0..<topmore.count {
            let moreModel:HomeHttpInfoModelTitleContentModel = topmore[index] as! HomeHttpInfoModelTitleContentModel
            headlinesMoreCellModel.moreImageUrl = moreModel.image
            headlinesMoreCellModel.pageUrl = moreModel.comments
        }
        for index in 0..<toptitle.count {
            let moreModel:HomeHttpInfoModelTitleContentModel = toptitle[index] as! HomeHttpInfoModelTitleContentModel
            headlinesMoreCellModel.titleImageUrl = moreModel.image
        }
        headlinesMoreCellModel.block = {[weak self] in
            self?.actionGotoWeb(webViewUrl: (self?.headlinesMoreCellModel.pageUrl!)!, isHomePushType: .YesIsHomePushType)
        }
        headlinesTitleArray .add(headlinesMoreCellModel)
        if topmore.count != 0 && toptitle.count != 0 {
            self.dataSource .add(headlinesTitleArray)
        }
    }
    //添加上药头条
    func addHeadlinesMore(topnews:NSMutableArray){
        headlinesArray = NSMutableArray()
        for index in 0..<topnews.count {
            let titleContent:HomeHttpInfoModelTitleContentModel = topnews[index] as! HomeHttpInfoModelTitleContentModel
            self.addHttpAdvisoryTableViewCellModel(array: headlinesArray, httpModel: titleContent)
        }
        self.dataSource .add(headlinesArray)
    }
    
//    func addHeadlinesMoreDetailedTableViewCellModel(array:NSMutableArray,httpModel:HomeHttpInfoModelTitleContentModel){
//        let model = HeadlinesMoreDetailedTableViewCellModel()
//        model.className = "HeadlinesMoreDetailedTableViewCell"
//        model.timeStr = httpModel.publishtime
//        model.titleStr = judgmentStrNil(str: httpModel.title)
//        model.imageUrl = httpModel.image
//        model.block = {[weak self] in
//            self?.openWeb(httpModel: httpModel)
//        }
//        array .add(model)
//    }
    func addHttpAdvisoryTableViewCellModel(array:NSMutableArray,httpModel:HomeHttpInfoModelTitleContentModel){
        let model = AdvisoryTableViewCellModel()
        model.className = "AdvisoryTableViewCell"
        //        model.timeStr = httpModel.createon
        model.timeStr = httpModel.publishtime
        model.titleStr = httpModel.title
        model.imageUrl = httpModel.image
        model.block = {[weak self] in
            self?.openWeb(httpModel: httpModel)
        }
        array .add(model)
    }
    func openWeb(httpModel:HomeHttpInfoModelTitleContentModel){
        let url:String = AccountInfo.sharedInstance().getAdvisoryVCUrl()+httpModel.id!
        //value1 备选页面
        if httpModel.value1?.count != 0 {
            self.actionGotoTitleWeb(webViewUrl: httpModel.value1!, title: "上药头条", isHomePushType: .YesIsHomePushType)
        }else{
            self.actionGotoTitleWeb(webViewUrl: url, title: "上药头条", isHomePushType: .YesIsHomePushType)
        }
    }
}
