//
//  tabBar.swift
//  Garmentoo
//
//  Created by Vijith on 24/03/22.
//
import UIKit
/*

class CustomTabBar: UITabBar {

     override func awakeFromNib() {
            super.awakeFromNib()
        
        
            layer.masksToBounds = false
            layer.cornerRadius = 20
            layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
        layer.shadowColor = UIColor.black.cgColor
       layer.shadowRadius = 2
        layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: layer.cornerRadius).cgPath
       layer.shadowOffset = CGSize(width: 0, height: -1)
     layer.shadowOpacity = 1
      }
 }
*/
@IBDesignable class CustomTabBar: UITabBar {
    @IBInspectable var color: UIColor?
    @IBInspectable var radii: CGFloat = 20.0
    
//    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
//        addShape()
//    }
    
//
//        var background: UIColor {
//            if #available(iOS 13, *) {
//                return UIColor { (traitCollection: UITraitCollection) -> UIColor in
//                    if traitCollection.userInterfaceStyle == .dark {
//                        return
//                            UIColor(white: 0.2, alpha: 1.0)
//                    } else {
//                        return UIColor.tabcolor
//                    }
//                }
//            } else {
//                return UIColor.tabcolor
//
//            }
//        }
    

    private var shapeLayer: CALayer?

    override func draw(_ rect: CGRect) {
        addShape()
    }

    private func addShape() {
        let shapeLayer = CAShapeLayer()

        shapeLayer.path = createPath()
        shapeLayer.strokeColor = UIColor.gray.withAlphaComponent(0.1).cgColor
       // shapeLayer.fillColor = color?.cgColor ?? UIColor.white.cgColor
        shapeLayer.fillColor = UIColor.tabbackground.cgColor
        shapeLayer.lineWidth = 2
        shapeLayer.shadowColor = UIColor.tabcolor.cgColor
        shapeLayer.shadowOffset = CGSize(width: 0   , height: -1);
        shapeLayer.shadowOpacity = 0.2
        shapeLayer.shadowPath =  UIBezierPath(roundedRect: bounds, cornerRadius: radii).cgPath
        
        

        if let oldShapeLayer = self.shapeLayer {
            layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
        } else {
            layer.insertSublayer(shapeLayer, at: 0)
        }

        self.shapeLayer = shapeLayer
    }

    private func createPath() -> CGPath {
        let path = UIBezierPath(
            roundedRect: bounds,
            byRoundingCorners: [.topLeft, .topRight],
            cornerRadii: CGSize(width: radii, height: 0.0))

        return path.cgPath
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.isTranslucent = true
        var tabFrame            = self.frame
        tabFrame.size.height    = 65 + (UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? CGFloat.zero)
        tabFrame.origin.y       = self.frame.origin.y +   ( self.frame.height - 65 - (UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? CGFloat.zero))
        self.layer.cornerRadius = 20
        self.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
        self.frame            = tabFrame



        self.items?.forEach({ $0.titlePositionAdjustment = UIOffset(horizontal: 0.0, vertical: -5.0) })


    }

}
