//
// © 2017-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

import UIKit

enum Theming {
    static let navBarColor = UIColor(white: 33.0/255.0, alpha: 1.0)
    static let navBarFontSize: CGFloat = 21.0
    static let navBarFont = UIFont.systemFont(ofSize: navBarFontSize, weight: .semibold)
        
    static func themeUIComponents () {
        UINavigationBar.appearance().barTintColor = navBarColor
        UINavigationBar.appearance().isTranslucent = false
        
        let titleAttr = [NSAttributedString.Key.foregroundColor: UIColor.white,
                         NSAttributedString.Key.font: navBarFont as Any]
        UINavigationBar.appearance().titleTextAttributes = titleAttr
        
        
        // Back button fun
        
        // Get rid of chevron
        UINavigationBar.appearance().backIndicatorImage = UIImage()
        UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage()
        
        if let backButtonImage = UIImage(named: "btn-back") {
            let modifiedBackButtonImage = backButtonImage.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: backButtonImage.size.width-1, bottom: 0, right: 0))
            UIBarButtonItem.appearance().setBackButtonBackgroundImage(modifiedBackButtonImage, for: .normal, barMetrics: .default)
        }
        
        if let backButtonPressedImage = UIImage(named: "btn-back-pressed") {
            let modifiedBackButtonPressedImage = backButtonPressedImage.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: backButtonPressedImage.size.width-1, bottom: 0, right: 0))
            UIBarButtonItem.appearance().setBackButtonBackgroundImage(modifiedBackButtonPressedImage, for: .highlighted , barMetrics: .default)
        }
    }
}

// MARK: -

extension UISwitch {
	private static let switchThumbOnColor = UIColor(red: 77/255.0, green: 176/255.0, blue: 215/255.0, alpha: 1.0)
	private static let switchThumbOffColor = UIColor(white: 241/255.0, alpha: 1.0)
	private static let switchBGOnColor = UIColor(red: 179/255.0, green: 200/255.0, blue: 205/255.0, alpha: 1.0)
	private static let switchBGOffColor = UIColor(white: 187/255.0, alpha: 1.0)
	
	func themeForJukebox() {
		transform = CGAffineTransform(scaleX: 0.85, y: 0.85)
		layer.cornerRadius = 16.0
		layer.masksToBounds = true
		tintColor = UISwitch.switchBGOffColor
		onTintColor = UISwitch.switchBGOnColor
	}
	
	func updateStylingForStateForJukebox() {
		thumbTintColor = isOn ? UISwitch.switchThumbOnColor : UISwitch.switchThumbOffColor
		backgroundColor = isOn ? UISwitch.switchBGOnColor : UISwitch.switchBGOffColor
	}
}

enum ColorCompat {
    static var label: UIColor {
        if #available(iOS 13.0, *) {
            return UIColor.label
        } else {
            return UIColor.black
        }
    }

    static var secondaryLabel: UIColor {
        if #available(iOS 13.0, *) {
            return UIColor.secondaryLabel
        } else {
            return UIColor(white: 0.23, alpha: 0.6)
        }
    }

    static var separator: UIColor {
        if #available(iOS 13.0, *) {
            return UIColor.separator
        } else {
            return UIColor(white: 0.23, alpha: 0.3)
        }
    }

    static var systemGroupedBackground: UIColor {
        if #available(iOS 13.0, *) {
            return UIColor.systemGroupedBackground
        } else {
            return UIColor(white: 0.96, alpha: 1.0)
        }
    }
}
