//
//  MenuFooterView.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 25.03.2021.
//

import UIKit

class MenuFooterView: UIView {
    //Private
    private let appImageView = UIImageView()
    private let appNameLabel = UILabel()
    private let versionLabel = UILabel()
    private var appVersion:String {
            if let infoDict: [String:Any] = Bundle.main.infoDictionary {
                let appVersion = infoDict["CFBundleShortVersionString"] as? String
                return "\("menu.version".localized().capitalized) \(appVersion ?? "--")"
            }
            
            return "N/A"
        }
    
    init() {
        super.init(frame: .zero)
        
        prepareAppImageView()
        prepareLabels()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func updateUI() {
        backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        switch interfaceStyle {
        case .light:
            appNameLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            versionLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        case .dark:
            appNameLabel.textColor = ThemeManager.currentTheme.primaryTextColor
            versionLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        }
    }
}

//MARK:- Prepare
private extension MenuFooterView {
    func prepareAppImageView() {
        appImageView.contentMode = .scaleAspectFit
        appImageView.image = UIImage(named: "version_icon")
        addSubview(appImageView)
        
        appImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(40)
            make.top.equalToSuperview().inset(30).priority(999)
            make.left.equalToSuperview().inset(18)
            make.bottom.equalToSuperview()
        }
    }
    
    func prepareLabels() {
        appNameLabel.font = AppFont.SFPro.bold(size: 18)
        appNameLabel.text = Bundle.main.infoDictionary?[kCFBundleNameKey as String] as? String
        addSubview(appNameLabel)
        
        versionLabel.textAlignment = .right
        versionLabel.font = AppFont.SFPro.regular(size: 14)
        versionLabel.text = appVersion
        addSubview(versionLabel)
        
        //Constraints
        appNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(appImageView.snp.right).offset(18)
            make.centerY.equalTo(appImageView)
        }
        
        versionLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(18)
            make.centerY.equalTo(appImageView)
        }
    }
}
