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

import UIKit
enum IsAllType: Int{
    ///所有的Class
    case AllType = 1
    ///我的常用
    case OftenType = 2
}
enum EditStatusType: Int{
    //开始编辑
    case StartEditing = 1
    //完成编辑
    case FinishEditing = 2
}
enum OperationType: Int{
    case DeleteType = 1//可删除
    case AddType = 2//可添加
}
class MineMoreViewController: BaseViewController {

    lazy var myTableView: UITableView = {
        let myTableView = UITableView.init()
        if #available(iOS 11.0, *) {
            myTableView.contentInsetAdjustmentBehavior = .never
            myTableView.scrollIndicatorInsets = myTableView.contentInset
            myTableView.contentInset = UIEdgeInsetsMake(UIApplication.shared.statusBarFrame.size.height+44, 0, (self.navigationController?.navigationBar.height)!, 0)
        }else{
            myTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        registerCell(myTableView, cell: MineMoreTableViewCell.self)
        registerCell(myTableView, cell: MineMoreAllTableViewCell.self)
        
        myTableView.separatorStyle = .none
        myTableView.isScrollEnabled = false
        //self.myTableView.backgroundColor = grayBgColor
        setExtraCellLineHidden(tableView: myTableView)
        myTableView.backgroundColor = loginBg_Color
        myTableView.separatorStyle = .none
        setExtraCellLineHidden(tableView: myTableView)
        return myTableView
    }()
    
    
    var dataSource = NSMutableArray()
    var delegate:MineMoreViewControllerDelegate!
    
    var cellNum:Double!
    
    var editStatusType:EditStatusType = .FinishEditing
    var applicationType:ApplicationType = .MyApplicationType
    
    var leftN:UIBarButtonItem!
    var rightN:UIBarButtonItem!
    ///用户自定义
    var tableViewCellModel = MineMoreTableViewCellModel()
    ///所有的Class
    var tableViewCellModelTwo = MineMoreTableViewCellModel()
    ///所有 文字提示
    var tableViewCellAllModel:MineMoreAllTableViewCellModel! = nil
    ///用户自定义Class数组 网络请求
    var mineCellModelArray = NSMutableArray()
    ///所有Class数组 网络请求
    var mineTitleCellModelArray = NSMutableArray()
    
    var companyListModelArray = NSMutableArray()
    var departmentListModelArray = NSMutableArray()
    var selecClassModel:AdvisoryHttpListModel!
    

    override func viewDidLoad() {
        super.viewDidLoad()
        initRightBtn()
        //self.title = "更多"
        addInitTableView()
        actionHttpGCD()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //左侧返回按钮
    func initRightBtn(){
        leftN = UIBarButtonItem.init(image: UIImage.init(named: "return_image"), style: .plain, target: self, action: #selector(arrowResponse))
        rightN = UIBarButtonItem.init(title: "编辑", style: .plain, target: self, action: #selector(arrowResponses))
        self.navigationItem.leftBarButtonItem = leftN
        self.navigationItem.rightBarButtonItem = rightN
    }
    ///导航栏右侧按钮
    @objc func arrowResponses(){
        if self.editStatusType == .FinishEditing {
            rightN.title = "完成"
            leftN.image = UIImage.init(named: "")
            leftN.title = "取消"
            self.editStatusType = .StartEditing
        }else{
            rightN.title = "编辑"
            leftN.title = ""
            leftN.image = UIImage.init(named: "return_image")
            self.editStatusType = .FinishEditing
            getSaveApplicationsHttpCmd()
        }
        self.delegate.editStatusType = self.editStatusType
        self.myTableView.delegate = self.delegate
        self.myTableView.reloadData()
    }
    ///导航栏左侧按钮
    override func arrowResponse() {
        if self.editStatusType == .FinishEditing {
            self.navigationController?.popViewController(animated: true)
        }else{
            //取消
            self.editStatusType = .FinishEditing
            rightN.title = "编辑"
            leftN.title = ""
            leftN.image = UIImage.init(named: "return_image")
            self.delegate.editStatusType = self.editStatusType
            self.dataSource = NSMutableArray()
            tableViewCellModel.collcetionViewSectionModellArray = NSMutableArray()
            tableViewCellModelTwo.collcetionViewSectionModellArray = NSMutableArray()
            self.mineTitleCellModelArray  = NSMutableArray()
            self.actionHttpGCD()
            self.delegate.dataSource = self.dataSource
            self.myTableView.delegate = self.delegate
            self.myTableView.reloadData()
        }
    }
    ///获取所有文字提示
    func getTableViewCellAllModel(){
        tableViewCellAllModel = MineMoreAllTableViewCellModel()
        tableViewCellAllModel.className = "MineMoreAllTableViewCell"
        self.dataSource .add(tableViewCellAllModel)
    }
    override func gotoMessageVC(){
        self.actionGotoMessageWeb(messageId: AccountInfo.sharedInstance().messageId!, isHomePushType: .NOIsHomePushType)
    }
    func actionHttpGCD(){
        showHintView()
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getMyApplicationsHttpCmdTwo()
        })
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getMoreApplicationsTwoHttpCmd()
        })
        
        group.notify(queue: DispatchQueue.main) {[weak self] in
            self?.delegate.dataSource = self?.dataSource
            self?.myTableView.delegate = self?.delegate
            self?.myTableView.dataSource = self?.delegate
            self?.myTableView.reloadData()
            self?.hidenHintView()
        }
    }
}
