//
//  CustomerServiceRootVC.swift
//  MobilePortal
//
//  Created by caiyang on 2020/4/7.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

class CustomerServiceRootVC: BaseViewController,UIPageViewControllerDelegate,UIPageViewControllerDataSource {
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        btnViewWidth = SCREEN_WHIDTH()/CGFloat(cellNum)
        btnViewHeight = btnViewWidth*0.9
        //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)
        collectionView.collectionViewLayout = layout
        registerCollectinViewCell(collectionView, cell: UpcomingCollectionViewCell.self)
        registerCollectinViewCell(collectionView, cell: MineCollectionViewCell.self)
        collectionView.backgroundColor = UIColor.white
        return collectionView
    }()
    var dataSource:NSMutableArray!
    var delegate = UpcomingViewControllerDelegate()
    var layout = UICollectionViewFlowLayout()
    var cellNum:Double!
    var btnViewWidth = CGFloat()
    var btnViewHeight = CGFloat()
    
    var selecClassModel = ApplicationHttpListModel()
    var companyListModelArray = NSMutableArray()
    var departmentListModelArray = NSMutableArray()
    
    lazy var robotImageView: UIImageView = {
        let robotImageView = UIImageView.init(frame: CGRect(x:0, y: headBackgroundImageView.height-130, width:126.67 , height:157.67))
        robotImageView.contentMode = .scaleAspectFill
        robotImageView.image = UIImage.init(named: "robot_image")
        self.view.addSubview(robotImageView)
        return robotImageView
    }()
    
    lazy var headBackgroundImageView: UIImageView = {
        let headBackgroundImageView = UIImageView.init()
        headBackgroundImageView.contentMode = .scaleAspectFill
        headBackgroundImageView.image = UIImage.init(named: "headBackground_image")
        self.view.addSubview(headBackgroundImageView)
        return headBackgroundImageView
    }()
    /// 头部背景
    lazy var promptImageView: PromptImageView = {
        let promptImageView = PromptImageView.init(frame: CGRect(x:SCREEN_WHIDTH()-238.67, y: self.headBackgroundImageView.height-87.67, width:225 , height:67.67 ))
        return promptImageView
    }()
    /// 跳转智能客服
    lazy var contactBtnView: ContactBtnView = {
        let contactBtnView = ContactBtnView.init(frame: CGRect(x:0, y: SCREEN_HEIGHT()-(57+HEIGHT_BOTTOM_MARGIN()), width:225 , height:57+HEIGHT_BOTTOM_MARGIN() ))
        return contactBtnView
    }()
    lazy var segmentedPageViewController: HGSegmentedPageViewController = {
        let segmentedPageViewController = HGSegmentedPageViewController()
        segmentedPageViewController.categoryView.originalIndex = 0
        segmentedPageViewController.pageViewControllers = (self.pages as! [UIViewController])
        segmentedPageViewController.categoryView.titles = (sectionTitles as! [String])
        segmentedPageViewController.categoryView.titleNomalFont = UIFont.systemFont(ofSize: 14)
        segmentedPageViewController.categoryView.titleNormalColor = UIColorFromRGB(rgbValue: 0x707985)
        segmentedPageViewController.categoryView.titleSelectedFont = UIFont.init(name: "Helvetica-Bold", size: 15)
        segmentedPageViewController.categoryView.titleSelectedColor = UIColorFromRGB(rgbValue: 0x3D444F)
        segmentedPageViewController.categoryView.separator.isHidden = true
        return segmentedPageViewController
    }()
    lazy var segment:HMSegmentedControl = {
        let segment = HMSegmentedControl.init(frame: CGRect(x:0, y: self.headBackgroundImageView.height, width:SCREEN_WHIDTH() , height:46 ))
        segment.selectionIndicatorLocation = .none
        segment.selectionStyle = .textWidthStripe
        
//        segment.sectionTitles = ["订单类","单据类","售后类","发票类"]
        segment.selectionIndicatorColor = statusBarColor
        //        segment.selectionStyle = .fullWidthStripe
        //        segment.isUserDraggable = false
        segment.selectionIndicatorLocation = .down
        segment.titleTextAttributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 13.33),NSAttributedStringKey.foregroundColor:UIColorFromRGB(rgbValue: 0x707985)]
        segment.selectedTitleTextAttributes = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 15.33),NSAttributedStringKey.foregroundColor:UIColorFromRGB(rgbValue: 0x3D444F)]
        
        segment.selectionIndicatorHeight = 3.5
        segment.type = .text
        return segment
    }()
    
    var pages = NSMutableArray()
    var pageViewController:UIPageViewController!
    var currentPage:UInt = 0
    var index = 0
    
    var timeHotPage = NSInteger()
    var sectionTitles = NSMutableArray()
    var httpArray = NSMutableArray()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
//        wr_setDefaultStatusBarStyle
        self.wr_setNavBarBackgroundAlpha(0)
        self.wr_setStatusBarStyle(.lightContent)
        hideNavigationBar()
        self.appViewsSstatistics(title: "智能客服", scope: AppClickType.OftenAppType.rawValue)
        getUnReadNumberHttpPostCmd()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initTitleBar()
        
//        hideNavigationBar()
        self.title = "智能客服"
        cellNum = 4
        self.wr_setNavBarTitleColor(UIColor.white)
        self.wr_setNavBarShadowImageHidden(true)
        initHeadBackground()
        self.view .addSubview(collectionView)
//        self.view .addSubview(segment)
        self.view .addSubview(robotImageView)
        /// 添加练习客服按钮
        addContactBtnView()
//        setPageViewController()
        actionHttpGCD()
        // Do any additional setup after loading the view.
    }
    override func initReturnBtn(){
        let leftN:UIBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "customerReturn_image")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(arrowResponse))
        self.navigationItem.leftBarButtonItem = leftN
    }
    
    func actionHttpGCD(){
        showHintView()
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getCustomerServiewHttpCmd()
        })
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getAutonomousHttpPostCmd()
        })
        group.notify(queue: DispatchQueue.main) {[weak self] in
            self?.initDragCollectionView()
            self?.setHttpData()
//            self?.initPageController()
//            self?.setCurrentPage()
            self?.hidenHintView()
        }
    }
    /// 跳转详情
    func gotoDetalisVC(id:String)  {
        let vc:DetailsViewController = DetailsViewController()
        vc.detailsId = id
        self.pushViewController(viewController: vc, animated: true)
    }
    /// 跳转智能客服
   @objc func gotoDialogueVC()  {
        let vc:DialogueViewController = DialogueViewController()
        self.pushViewController(viewController: vc, animated: true)
    }
    /// 跳转自助服务更多
    func gotoMoreVC(){
        let vc = AutonmousMoreViewController()
        vc.hidesBottomBarWhenPushed = true
        self.pushViewController(viewController: vc, animated: true)
    }
    /// 应用访问量
    func appViewsSstatistics(title:String,scope:String){
        let model = ClickRateModel()
        model.name = title
        model.belong = "门户"
        model.scope = scope
        model.type = "1"
        self.getClickRateHttpCmd(infoModel: model)
    }
}
