//
//  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
}
/// 收回展开
enum RetractUnfoldType: Int{
    ///展开
    case Retractype = 1
    ///收回
    case UnfoldType = 2
}
/// 是否首页返回
enum SystemType: String{
    ///首页
    case HomeSystemType = "1"
    ///我的
    case MineSystemType = "0"
}
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)
            myTableView.contentInset = UIEdgeInsetsMake(UIApplication.shared.statusBarFrame.size.height+44, 0, HEIGHT_BOTTOM_MARGIN(), 0)
        }else{
            myTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        registerCell(myTableView, cell: MineMoreTableViewCell.self)
        registerCell(myTableView, cell: MineMoreAllTableViewCell.self)
        registerCell(myTableView, cell: NormalTableViewCell.self)
        registerCell(myTableView, cell: StartEditingTableViewCell.self)
        myTableView.separatorStyle = .none
        myTableView.isScrollEnabled = true
        //self.myTableView.backgroundColor = grayBgColor
        setExtraCellLineHidden(tableView: myTableView)
        myTableView.backgroundColor = UIColorFromRGB(rgbValue: 0xF4F9FD)
        return myTableView
    }()
    
    var systemType:SystemType = .MineSystemType
    
    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
    ///分割线
    var normal = NormalTableViewCellModel()
    ///开始编辑
    var startEditingCellModel = StartEditingTableViewCellModel()
    ///用户自定义Class数组 网络请求
    var mineCellModelArray = NSMutableArray()
    ///所有Class数组 网络请求
    var mineTitleCellModelArray = NSMutableArray()
    
    var companyListModelArray = NSMutableArray()
    var departmentListModelArray = NSMutableArray()
//    var selecClassModel:AdvisoryHttpListModel!
    var selecClassModel:ApplicationHttpListModel!
    
    /// 服务范围类型
    var scope:String?
    var scopes:String?
    
    var titleSub:String?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initRightBtn()
        self.titleSub = self.title
//        wr_setDefaultNavBarTintColor(UIColor.white)
        self.wr_setNavBarTintColor(UIColorFromRGB(rgbValue: 0x23262A))
        self.view.backgroundColor = UIColorFromRGB(rgbValue: 0xF4F9FD)
        //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(){
        let leftN = UIBarButtonItem.init(image: UIImage.init(named: "return_image")?.withRenderingMode(.alwaysOriginal), 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.title = "管理" + self.titleSub!
            self.editStatusType = .StartEditing
        }else{
//            rightN.title = "编辑"
//            leftN.title = ""
//            leftN.image = UIImage.init(named: "return_image")
            self.title = self.titleSub!
            rightN.title = ""
            self.editStatusType = .FinishEditing
            if AccountInfo.sharedInstance().oldType == "0" {
//                getSaveApplicationsHttpCmdOld()
            } else {
                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(title:String,isAllType:IsAllType){
        tableViewCellAllModel = MineMoreAllTableViewCellModel()
        tableViewCellAllModel.className = "MineMoreAllTableViewCell"
        tableViewCellAllModel.title = title
        if self.systemType == .HomeSystemType {
            tableViewCellAllModel.vcTitle = "首页"+self.title!
        } else {
            tableViewCellAllModel.vcTitle = self.title
        }
        tableViewCellAllModel.isAllType = isAllType
        tableViewCellAllModel.editStatusType = self.editStatusType
        self.dataSource .add(tableViewCellAllModel)
    }
    // 获取分割线
    func getNormalTableViewCellAllModel(){
        normal = NormalTableViewCellModel()
        normal.className = "NormalTableViewCell"
        self.dataSource .add(normal)
    }
    // 添加开始编辑按钮
    func getStartEditingTableViewCellModel(){
        startEditingCellModel = StartEditingTableViewCellModel()
        startEditingCellModel.className = "StartEditingTableViewCell"
        startEditingCellModel.block = {[weak self] in
            self?.arrowResponses()
        }
        self.dataSource .add(startEditingCellModel)
    }
    override func gotoMessageVC(){
        self.actionGotoMessageWeb(messageId: AccountInfo.sharedInstance().messageId!, isHomePushType: .NOIsHomePushType)
    }
    func actionHttpGCD(){
        if AccountInfo.sharedInstance().oldType == "0" {
            oldHttpAction()
        } else {
            newHttpAction()
        }
    }
    func newHttpAction(){
        showHintView()
        self.dataSource = NSMutableArray()
        queue.async(group: group, execute: {[weak self] in
            self?.group.enter()
            self?.getMyApplicationListHttpCmd()
        })
//        queue.sync {[weak self] in
//            self?.getMyApplicationListHttpCmd()
//        }
//        queue.async(group: group, execute: {[weak self] in
//            self?.group.enter()
//            self?.getAllApplicationHttpListCmd()
//        })
        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()
        }
    }
    func oldHttpAction(){
        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()
        }
    }
}
