//
//  SHBaseTabBarController.swift
//  CallRecording
//
//  Created by Mazy on 2020/7/13.
//  Copyright © 2020 Mazy. All rights reserved.
//

import UIKit

class SHBaseTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        tabBar.isTranslucent = false
        tabBar.tintColor = UIColor.darkGray
        tabBar.barStyle = .default
//        self.setValue(customTabBar, forKeyPath: "tabBar")
        
        if #available(iOS 10.0, *) {
            tabBar.unselectedItemTintColor = #colorLiteral(red: 0.7059153914, green: 0.7501759529, blue: 0.7668043971, alpha: 1)
        } else {
            // Fallback on earlier versions
            UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: #colorLiteral(red: 0.7059153914, green: 0.7501759529, blue: 0.7668043971, alpha: 1)], for: .normal)
        }
        
        
        // change top line color
        tabBar.layer.borderWidth = 0.3
        tabBar.layer.borderColor = #colorLiteral(red: 0.7059153914, green: 0.7501759529, blue: 0.7668043971, alpha: 1).cgColor
        tabBar.clipsToBounds = true
        
        configControllers()
        
        self.tabBar.isHidden = true
    }
    
    /// MARK: - 设置子控制器
    func configControllers() {
        
//        let recordVC = UIStoryboard.init(name: "Record", bundle: nil).instantiateViewController(withIdentifier: "SHRecordViewController") as! SHRecordViewController
//        addChildVC(vc: recordVC, title: "", image: nil, selectedImage: nil)
        
        let recordVC = UIStoryboard.init(name: "Record", bundle: nil).instantiateViewController(withIdentifier: "SHRecordListViewController") as! SHRecordListViewController
        addChildVC(vc: recordVC, title: "", image: nil, selectedImage: nil)
    }
    
    /// 添加子控制器
    ///
    /// - Parameters:
    ///   - vc: 控制器
    ///   - title: 标题
    ///   - image: 默认图片
    ///   - selectedImage: 选中图片
    private func addChildVC(vc: UIViewController, title: String?, image: UIImage?, selectedImage: UIImage?) {
        vc.navigationItem.title = title
//        vc.title = title
        vc.tabBarItem.image = image?.withRenderingMode(.alwaysOriginal)
        if #available(iOS 10, *) {
            vc.tabBarItem.imageInsets.bottom = -12
        } else {
            vc.tabBarItem.imageInsets.bottom = -6
            vc.tabBarItem.imageInsets.top = 6
        }
        
        vc.tabBarItem.selectedImage = selectedImage?.withRenderingMode(.alwaysOriginal)
        let mainNavi = SHBaseNavigationController(rootViewController: vc)
        self.addChild(mainNavi)
    }
}
