//
//  TodayAdCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 12.02.2021.
//

import UIKit

class TodayAdCell: UITableViewCell {
    //Private
    private let container = UIView()
    private let label = UILabel()
    private let gradientView = GradientView(startColor: UIColor.white.withAlphaComponent(0),
                                            endColor: UIColor(hex: 0xdaddec),
                                            opacity: 0.5)
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCellStyle()
        prepareContainer()
        prepareAd()
        prepareGradient()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUI()
    }
    
    private func updateUI() {
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        container.backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        label.textColor = ThemeManager.currentTheme.primaryTextColor
        
        switch interfaceStyle {
        case .light:
            gradientView.isHidden = false
            container.layer.borderWidth = 1 / UIScreen.main.scale
            container.layer.borderColor = UIColor(hex: 0xc7c7c7).cgColor
        case .dark:
            gradientView.isHidden = true
            container.layer.borderWidth = 0
        }
    }
}

//MARK:- Prepare
private extension TodayAdCell {
    func prepareCellStyle() {
        selectionStyle = .none
        
    }
    
    func prepareContainer() {
        container.layer.cornerRadius = 6
        contentView.addSubview(container)
        
        container.snp.makeConstraints { (make) in
            make.height.equalTo(64)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview().inset(18)
            make.left.right.equalToSuperview().inset(18)
        }
    }
    
    func prepareAd() {
        label.text = "Advertisment"
        label.font = AppFont.SFPro.regular(size: 14)
        container.addSubview(label)
        
        label.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
    }
    
    func prepareGradient() {
        contentView.addSubview(gradientView)
        contentView.bringSubviewToFront(container)
        gradientView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalToSuperview().multipliedBy(0.5)
        }
    }
}
