//
//  ArrowButton.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 18.02.2021.
//

import UIKit

class ArrowButton: UIButton {
    //Private
    private let gradientBackground = GradientView(startColor: UIColor.white,
                                                  endColor: UIColor(hex: 0xeceef6))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareButton()
        prepareBackground()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    private func updateUI() {
        backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        switch interfaceStyle {
        case .light:
            gradientBackground.isHidden = false
            tintColor = UIColor(hex: 0x161626)
            layer.shadowOpacity = 0.12
        case .dark:
            gradientBackground.isHidden = true
            tintColor = ThemeManager.currentTheme.secondaryTextColor
            layer.shadowOpacity = 0
        }
    }
}

//MARK:- Prepare
private extension ArrowButton {
    func prepareButton() {
        let arrowImage = UIImage(named: "down_arrow")
        setImage(arrowImage, for: .normal)
        transform = CGAffineTransform.init(rotationAngle: -90 * .pi / 180)
        
        clipsToBounds = false
        layer.cornerRadius = 9
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOffset = .init(width: 0, height: 3)
        layer.shadowRadius = 8
    }
    
    func prepareBackground() {
        gradientBackground.layer.cornerRadius = 9
        insertSubview(gradientBackground, belowSubview: imageView!)
        
        gradientBackground.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}
