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

import UIKit

class RootTabBarViewController: UITabBarController,UITabBarControllerDelegate {
    var markNavgation :UINavigationController!
    override func viewDidLoad() {
        super.viewDidLoad()
        loadNCS()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func loadNCS(){
        let rootBarViewControllers:NSArray = [self.gainHome(),self.gainService(),self.gainAdvisory(),gainMine()]
        markNavgation = rootBarViewControllers[0] as? UINavigationController
        for index in 0..<rootBarViewControllers.count {
            self.addChildViewController(rootBarViewControllers[index] as! UIViewController)
        }
       
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColorFromRGB(rgbValue: 0x4F5762),NSAttributedStringKey.font:UIFont.boldSystemFont(ofSize: 12)], for:UIControlState.normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColorFromRGB(rgbValue: 0x2D7CED),NSAttributedStringKey.font:UIFont.boldSystemFont(ofSize: 12)], for: UIControlState.selected)
        
        self.tabBar.tintColor = grayTabbarColor
        self.delegate = self
//        self.selectedIndex = 0
        let myTabbar = MyTabbar.init(frame: CGRect(x:0, y: 0, width:self.tabBar.width , height:self.tabBar.height+HEIGHT_BOTTOM_MARGIN()))
        myTabbar.actionBlock = {
//            NotificationCenter.default.post(name: Notification.Name(rawValue: "gotoCustomerServiceVC"), object: "0")
            NotificationCenter.default.post(name: Notification.Name(rawValue: "gotoIntelligentCustomerService"), object: "0")
            
        }
        myTabbar.isTranslucent = false
        myTabbar.delegate = self
        self.setValue(myTabbar, forKey: "tabBar")
        let bgView:UIView = UIView.init(frame: CGRect(x:0, y: 0, width:self.tabBar.width , height:self.tabBar.height+HEIGHT_BOTTOM_MARGIN()))
        
//        bgView.backgroundColor = statusBarColor
        bgView.backgroundColor = UIColor.white
        myTabbar.insertSubview(bgView, at: 0)
        self.tabBar.isOpaque = true
    }
    func getNavigationWithVc(vc:UIViewController,title:String,img1:String,selectImg:String)->UINavigationController{
        let nav:UINavigationController=UINavigationController.init(rootViewController: vc)
        var img:UIImage=UIImage(named: img1)!
        img=img.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        var imgSelect:UIImage=UIImage(named: selectImg)!
        imgSelect=imgSelect.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        nav.tabBarItem=UITabBarItem.init(title: title, image: img, selectedImage: imgSelect)
        return nav
    }
    func gainHome()->UINavigationController{
        let homevc:HomeViewController=HomeViewController()
//        let homevc:HomeDicViewController = HomeDicViewController()
        
//        let homevc:SelectCompanyViewController = SelectCompanyViewController.createViewController(createArgs: nil) as! SelectCompanyViewController
        return self.getNavigationWithVc(vc: homevc, title: "首页", img1: "tabbar_one_nor", selectImg:"tabbar_one_select")
    }
    func gainAdvisory()->UINavigationController{
        let vc:AdvisoryRootViewController=AdvisoryRootViewController()
        
        return self.getNavigationWithVc(vc: vc, title: "资讯", img1: "tabbar_two_nor", selectImg:"tabbar_two_select")
    }
    
    func gainService()->UINavigationController{
//        let vc:ServiceViewController=ServiceViewController()
        let vc:UpcomingViewController = UpcomingViewController()
         return self.getNavigationWithVc(vc: vc, title: "待办", img1: "tabbar_three_nor", selectImg:"tabbar_three_select")
    }
    
    func gainMine()->UINavigationController{
        let vc:MineViewController=MineViewController()
        
        return self.getNavigationWithVc(vc: vc, title: "我的", img1: "tabbar_for_nor", selectImg:"tabbar_for_select")
        
    }
}
