//
//  BaseController.swift
//  SwiftProject
//
//  Created by soldoros on 2024/1/10.
//

import UIKit


class BaseController: BaseVirtualController {
  
    //是否是根控制器
    var _isRoot:Bool?
    //导航栏标题
    var _titleString:NSString?
    
    init() {
        super.init(nibName: nil, bundle: nil)
        _isRoot = false
    }
    
    init(isRoot:Bool) {
        super.init(nibName: nil, bundle: nil)
        _isRoot = isRoot
    }
    
    init(isRoot:Bool, titleString:NSString) {
        super.init(nibName: nil, bundle: nil)
        _isRoot = isRoot
        _titleString = titleString
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var prefersStatusBarHidden: Bool{
        return false
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if(self.navtionBar != nil){
            self.view.bringSubviewToFront(self.navtionBar!)
        }
        
        self.navigationController?.navigationBar.isHidden = true;
        self.navigationController?.navigationBar.isTranslucent = false;
        
        if(_isRoot == true){
            self.navigationController?.tabBarController!.tabBar.isHidden = false;
            self.navigationController?.tabBarController!.tabBar.isTranslucent = false;
        }
        
        
        //适配ios 11  滚动视图返回时有偏移 reloadData的时候，会重新计算contentSize，就有可能会引起contentOffset的变化 在这里关闭
        for subView:UIView in view.subviews{
            if(subView .isKind(of: UITableView.classForCoder())){
                let tableView:UITableView = subView as! UITableView
                if #available(iOS 11.0, *) {
                    tableView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
                }
                else {
                    self.automaticallyAdjustsScrollViewInsets = false
                }
                
                tableView.scrollIndicatorInsets = tableView.contentInset;
                let scro:UIScrollView = subView as! UIScrollView
                scro.scrollIndicatorInsets = tableView.contentInset;
            }
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setNavgationBarColorImg(color: tabBarTintSelectColor)
        self.setNavgationBarLine(color: BackGroundColor)
        self.setNavgationTitleAttribute(font: UIFont.systemFont(ofSize: 18), color: UIColor.hexStringToColor(hexString: "#ffffff"))
        if(_isRoot == false){
            self.setLeftOneBtnImg(imgStr: "return")
          
//            self.navLine?.isHidden = true
//            self.navtionBar!.layer.shadowColor = UIColor.colorWithRGB(_r: 0, _g: 0, _b: 0, alpha: 0.08).cgColor
//            self.navtionBar!.layer.shadowOffset = CGSize.init(width: 0, height: 3)
//            self.navtionBar!.layer.shadowOpacity = 1;
//            self.navtionBar!.layer.shadowRadius = 4;
        }
        
    }
    
    
    override func navgationButtonClick(sender: UIButton) {
        if(sender == self.leftBtn1){
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    ///消除导航栏下划线
        func clearNavigationBarLine() {
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
            self.navigationController?.navigationBar.shadowImage = UIImage()
        }
        
        ///消除标签栏下划线
        func clearTabBarLine() {
            let image_w = creatColorImage(.white)
            if #available(iOS 13.0, *) {
                let appearance = UITabBarAppearance()
                appearance.backgroundImage = image_w
                appearance.shadowImage = image_w
                self.tabBarController?.tabBar.standardAppearance = appearance
            } else {
                self.tabBarController?.tabBar.backgroundImage = image_w
                self.tabBarController?.tabBar.shadowImage = image_w
            }
        }
    
    ///用颜色创建一张图片
        func creatColorImage(_ color:UIColor,_ ARect:CGRect = CGRect.init(x: 0, y: 0, width: 1, height: 1)) -> UIImage {
            let rect = ARect
            UIGraphicsBeginImageContext(rect.size)
            let context = UIGraphicsGetCurrentContext()
            context?.setFillColor(color.cgColor)
            context?.fill(rect)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image!
        }
    
    
}
