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

import UIKit

class MenuBuyButton: UIControl {
    //Private
    private let label = UILabel()
    private let gradient = CAGradientLayer()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareButton()
        prepareGradient()
        prepareLabel()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        gradient.frame = self.bounds
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        
        UIView.performWithoutAnimation {
            self.alpha = 0.7
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        
        UIView.performWithoutAnimation {
            self.alpha = 1
        }
    }
}

//MARK:- Prepare
private extension MenuBuyButton {
    func prepareButton() {
        layer.cornerRadius = 4
    }
    
    func prepareGradient() {
        gradient.cornerRadius = self.layer.cornerRadius
        gradient.frame = self.bounds
        gradient.colors = [UIColor(hex: 0x5e77ff).cgColor, UIColor(hex: 0x97abf4).cgColor]
        gradient.startPoint = .init(x: 0, y: 0.5)
        gradient.endPoint = .init(x: 1, y: 0.5)
        layer.addSublayer(gradient)
    }
    
    func prepareLabel() {
        label.textColor = .white
        label.font = AppFont.SFPro.regular(size: 18)
        label.text = "menu.buyNow".localized()
        label.setContentHuggingPriority(.fittingSizeLevel, for: .vertical)
        addSubview(label)
        
        label.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview().inset(8).priority(.init(999))
            make.left.right.equalToSuperview().inset(12)
        }
    }
}
