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

import UIKit

class MenuHeaderView: UIView {
    //Private
    private let premiumContainer = UIView()
    private let premiumHeadingLabel = UILabel()
    private let premiumDescriptionLabel = UILabel()
    private let buyButton = MenuBuyButton()
    private let premiumImageView = UIImageView()
    
    private let tvButton = MenuHeaderButton(image: UIImage(named: "menu_tv"), text: "menu.tv".localized())
    private let radarButton = MenuHeaderButton(image: UIImage(named: "menu_radar"), text: "menu.radar".localized())
    
    init() {
        super.init(frame: .zero)
        
        prepareView()
        preparePremium()
        prepareButtons()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    //Prvaite
    private func updateUI() {
        backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        premiumContainer.backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        premiumHeadingLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        premiumDescriptionLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        
        switch interfaceStyle {
        case .light:
            premiumContainer.layer.shadowColor = UIColor(hex: 0x020116).cgColor
            premiumContainer.layer.shadowOffset = .init(width: 0, height: 10)
            premiumContainer.layer.shadowRadius = 20
            premiumContainer.layer.shadowOpacity = 0.12
        case .dark:
            premiumContainer.layer.shadowColor = UIColor.black.cgColor
            premiumContainer.layer.shadowOffset = .init(width: 8, height: 8)
            premiumContainer.layer.shadowRadius = 8
            premiumContainer.layer.shadowOpacity = 1.0
        }
    }
    
    @objc private func handleBuyButton() {
        
    }
    
    @objc private func handleButton(button:MenuHeaderButton) {
        
    }
}

//MARK:- Prepare
private extension MenuHeaderView {
    func prepareView() {
        backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
    }
    
    func preparePremium() {
        premiumContainer.layer.cornerRadius = 12
        addSubview(premiumContainer)
        
        premiumHeadingLabel.font = AppFont.SFPro.bold(size: 34)
        premiumHeadingLabel.text = "menu.goPremium".localized()
        premiumHeadingLabel.setContentHuggingPriority(.fittingSizeLevel, for: .vertical)
        premiumContainer.addSubview(premiumHeadingLabel)
        
        premiumDescriptionLabel.lineBreakMode = .byWordWrapping
        premiumDescriptionLabel.numberOfLines = 0
        premiumDescriptionLabel.font = AppFont.SFPro.regular(size: 16)
        premiumDescriptionLabel.text = "menu.premium.desc".localized()
        premiumDescriptionLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
        premiumContainer.addSubview(premiumDescriptionLabel)
        
        buyButton.addTarget(self, action: #selector(handleBuyButton), for: .touchUpInside)
        premiumContainer.addSubview(buyButton)
        
        premiumImageView.contentMode = .scaleAspectFit
        premiumImageView.image = UIImage(named: "premium")
        premiumContainer.addSubview(premiumImageView)
        
        //Constraints
        premiumContainer.snp.makeConstraints { (make) in
            make.top.equalTo(30)
            make.left.right.equalToSuperview().inset(18).priority(999)
        }
        
        premiumImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(140)
            make.right.bottom.equalToSuperview()
        }
        
        premiumHeadingLabel.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().inset(18)
        }
        
        premiumDescriptionLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(18)
            make.top.equalTo(premiumHeadingLabel.snp.bottom)
            make.right.equalTo(premiumImageView.snp.left)
        }
        
        buyButton.snp.makeConstraints { (make) in
            make.top.equalTo(premiumDescriptionLabel.snp.bottom).offset(18).priority(999)
            make.left.equalToSuperview().inset(18)
            make.bottom.equalToSuperview().inset(24)
        }
    }
    
    func prepareButtons() {
        tvButton.addTarget(self, action: #selector(handleButton(button:)), for: .touchUpInside)
        addSubview(tvButton)
        
        radarButton.addTarget(self, action: #selector(handleButton(button:)), for: .touchUpInside)
        addSubview(radarButton)
        
        //Constraints
        tvButton.snp.makeConstraints { (make) in
            make.left.equalTo(premiumContainer)
            make.top.equalTo(premiumContainer.snp.bottom).offset(30).priority(999)
            make.width.equalTo(165)
            make.bottom.equalToSuperview().inset(30)
        }
        
        radarButton.snp.makeConstraints { (make) in
            make.right.equalTo(premiumContainer)
            make.top.equalTo(premiumContainer.snp.bottom).offset(30).priority(999)
            make.width.equalTo(165)
            make.bottom.equalToSuperview().inset(30)
        }
    }
}
