//
//  ServiceViewController.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/11.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

class ServiceViewController: BaseViewController {
   
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        btnViewWidth = SCREEN_WHIDTH()/CGFloat(cellNum)
        btnViewHeight = btnViewWidth
        //self.dataSource = xinfo.detaileArray
        //最小行间距
        layout.minimumLineSpacing = 0
        //设置垂直间距
        layout.minimumInteritemSpacing = 0
        //设置 cell 上下左右的间距
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.itemSize = CGSize(width:btnViewWidth, height:btnViewHeight)
        //layout.headerReferenceSize = CGSize(width: SCREEN_WHIDTH(), height: 26*HEIGHT_RATIO())
        
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
            collectionView.scrollIndicatorInsets = collectionView.contentInset
            collectionView.contentInset = UIEdgeInsetsMake(UIApplication.shared.statusBarFrame.size.height+44, 0, HEIGHT_TAB_BAR()+HEIGHT_BOTTOM_MARGIN(), 0)
        }else{
            collectionView.contentInset = UIEdgeInsetsMake(44+UIApplication.shared.statusBarFrame.size.height, 0, 0, 0)
        }
        collectionView.collectionViewLayout = layout
        registerCollectinViewCell(collectionView, cell: ServiceCollectionViewCell.self)
        collectionView.backgroundColor = UIColor.clear
        return collectionView
    }()
    
    var dataSource:NSMutableArray!
    var delegate = ServiceViewControllerDelegate()
    var layout = UICollectionViewFlowLayout()
    var cellNum:Double!
    var btnViewWidth = CGFloat()
    var btnViewHeight = CGFloat()
    
    //顶部刷新
    var header = MJRefreshNormalHeader()
    //底部刷新
    var footer = MJRefreshAutoNormalFooter()
    var httpListData = ServiceHttpListModelData()
    lazy var bagView: UIImageView = {
        let bagView = UIImageView.init(frame: CGRect(x:0, y: SCREEN_HEIGHT()-300*HEIGHT_RATIO()-HEIGHT_TAB_BAR()-HEIGHT_BOTTOM_MARGIN(), width:SCREEN_WHIDTH() , height:280*HEIGHT_RATIO() ))
        bagView.contentMode = .scaleAspectFit
        bagView.image = UIImage.init(named: "waitan_image")
        //bagView.backgroundColor = UIColor.red
        return bagView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initTitleBar()
        initReturnBtn()
        self.title = "服务"
        self.view.addSubview(bagView)
        initDragCollectionView()
        actionHttpGCD()
        addHeaderFooter()
        
        // Do any additional setup after loading the view.
    }
    //左侧返回按钮
    override func initReturnBtn(){
        
        let leftN:UIBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: ""), style: .plain, target: self, action: #selector(arrowResponse))
        self.navigationItem.leftBarButtonItem = leftN
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //MARK:添加上拉加载 下拉刷新
    func addHeaderFooter(){
        self.header.setRefreshingTarget(self, refreshingAction: #selector(upPullLoadData))
        self.header.lastUpdatedTimeLabel.isHidden = true
        self.header.stateLabel.isHidden = true
        self.footer.setRefreshingTarget(self, refreshingAction: #selector(downPlullLoadData))
        self.footer.stateLabel.isHidden = true
        self.footer.isRefreshingTitleHidden = true
        self.collectionView.mj_header = self.header
        //self.collectionView.mj_footer = self.footer
    }
    //MARK:下拉刷新
    @objc func upPullLoadData(){
        actionHttpGCD()
    }
    //MARK:上拉加载
    @objc func downPlullLoadData(){
        
    }
    func actionHttpGCD(){
        showHintView()
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getServiceHttpCmd()
        })
        group.notify(queue: DispatchQueue.main) {[weak self] in
            self?.delegate.dataSource = self?.dataSource
            self?.collectionView.delegate = self?.delegate
            self?.collectionView.dataSource = self?.delegate
            self?.collectionView.reloadData()
            self?.collectionView.mj_header.endRefreshing()
            self?.hidenHintView()
        }
    }
    override func gotoMessageVC(){
        self.actionGotoMessageWeb(messageId: AccountInfo.sharedInstance().messageId!, isHomePushType: .YesIsHomePushType)
    }

}
