//
//  NavigationCityButton.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 08.02.2021.
//

import UIKit

class NavigationCityButton: UIControl {
    //Private
    private let contentView = UIView()
    private let pinImageView = UIImageView()
    private let cityLabel = UILabel()
    private let downArrowImageView = UIImageView()
    private let timeLabel = UILabel()
    private let kSpaceForDownArrow:CGFloat = 10
    private static let timeFormatter:DateFormatter = {
        let fmt = DateFormatter()
        fmt.dateFormat = "h:mm a"
        
        return fmt
    }()
    
    init() {
        super.init(frame: .zero)
        
        prepareContentView()
        prepareTitle()
        preparePinImage()
        prepareDownArrow()
        prepareTimeLabel()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        
        let highlightedColor = ThemeManager.currentTheme.primaryTextColor.highlighted
        downArrowImageView.tintColor = highlightedColor
        pinImageView.tintColor = highlightedColor
        cityLabel.textColor = highlightedColor
        timeLabel.textColor = highlightedColor
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        
        let normalColor = ThemeManager.currentTheme.primaryTextColor
        downArrowImageView.tintColor = normalColor
        pinImageView.tintColor = normalColor
        cityLabel.textColor = normalColor
        timeLabel.textColor = normalColor
    }
    
    public func configure(with location:Location?) {
        guard let loc = location else {
            self.cityLabel.text = "N/A"
            self.timeLabel.text = nil
            return
        }
        
        self.cityLabel.text = loc.cityName
        if let today = loc.today?.date {
            NavigationCityButton.timeFormatter.timeZone = loc.timeZone
            self.timeLabel.text = NavigationCityButton.timeFormatter.string(from: today)
        }
        else {
            self.timeLabel.text = nil
        }
    }
}

//MARK:- Prepare
private extension NavigationCityButton {
    func prepareContentView() {
        contentView.isUserInteractionEnabled = false
        contentView.backgroundColor = .clear
        addSubview(contentView)
        
        contentView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func prepareTitle() {
        cityLabel.isUserInteractionEnabled = false
        cityLabel.font = AppFont.SFPro.bold(size: 16)
        cityLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        cityLabel.textAlignment = .left
        contentView.addSubview(cityLabel)
        
        cityLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
        }
    }
    
    func preparePinImage() {
        pinImageView.isUserInteractionEnabled = false
        pinImageView.image = UIImage(named: "city_pin")
        pinImageView.contentMode = .scaleAspectFit
        pinImageView.tintColor = ThemeManager.currentTheme.primaryTextColor
        contentView.addSubview(pinImageView)
        
        pinImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalTo(cityLabel.snp.left).offset(-10)
            make.centerY.equalTo(cityLabel)
            make.size.equalTo(CGSize(width: 12, height: 14))
        }
    }
    
    func prepareDownArrow() {
        downArrowImageView.isUserInteractionEnabled = false
        downArrowImageView.image = UIImage(named: "down_arrow")
        downArrowImageView.tintColor = ThemeManager.currentTheme.primaryTextColor
        downArrowImageView.contentMode = .scaleAspectFit
        contentView.addSubview(downArrowImageView)
        
        downArrowImageView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 14, height: 14))
            make.right.equalToSuperview()
            make.centerY.equalTo(cityLabel)
            make.left.equalTo(cityLabel.snp.right).offset(12)
        }
    }
    
    func prepareTimeLabel() {
        timeLabel.isUserInteractionEnabled = false
        timeLabel.font = AppFont.SFPro.regular(size: 14)
        timeLabel.textColor = ThemeManager.currentTheme.primaryTextColor
        timeLabel.sizeToFit()
        contentView.addSubview(timeLabel)
        
        timeLabel.snp.makeConstraints { (make) in
            make.top.lessThanOrEqualTo(cityLabel.snp.bottom).offset(3)
            make.top.equalTo(cityLabel.snp.bottom).offset(3)
            make.left.equalTo(cityLabel)
            make.bottom.equalToSuperview()
        }
    }
}

