//
//  TodayAlertCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 29.03.2021.
//

import UIKit

class TodayAlertCell: UITableViewCell {
    //Private
    private let container = UIView()
    private let alertImageView = UIImageView()
    private let infoLabel = UILabel()
    private let timeAgoLabel = UILabel()
    private let moreLabel = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareContainer()
        prepareImageView()
        prepareLabels()
        updateUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        self.updateUI()
    }
    
    private func updateUI() {
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
        container.backgroundColor = ThemeManager.currentTheme.containerBackgroundColor
        
        switch interfaceStyle {
        case .light:
            infoLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            timeAgoLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
            container.layer.shadowColor = UIColor(hex: 0x020116).cgColor
            container.layer.shadowOffset = .init(width: 0, height: 10)
            container.layer.shadowRadius = 20
            container.layer.shadowOpacity = 0.12
        case .dark:
            infoLabel.textColor = ThemeManager.currentTheme.primaryTextColor
            infoLabel.textColor = ThemeManager.currentTheme.primaryTextColor
            container.layer.shadowColor = UIColor.black.cgColor
            container.layer.shadowOffset = .init(width: 8, height: 8)
            container.layer.shadowRadius = 8
            container.layer.shadowOpacity = 1.0
        }
    }
}

//MARK:- Prepare
private extension TodayAlertCell {
    func prepareCell() {
        selectionStyle = .none
    }
    
    func prepareContainer() {
        container.layer.cornerRadius = 12
        contentView.addSubview(container)
        
        container.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(18)
        }
    }
    
    func prepareImageView() {
        alertImageView.image = UIImage(named: "alert")
        alertImageView.contentMode = .scaleAspectFit
        container.addSubview(alertImageView)
        
        alertImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(33)
            make.left.equalToSuperview().inset(8)
            make.top.bottom.equalToSuperview().inset(16)
        }
    }
    
    func prepareLabels() {
        infoLabel.numberOfLines = 2
        infoLabel.lineBreakMode = .byWordWrapping
        infoLabel.font = AppFont.SFPro.regular(size: 16)
        infoLabel.text = "Frost Advisory and 2 more alerts"
        container.addSubview(infoLabel)
        
        timeAgoLabel.font = AppFont.SFPro.regular(size: 14)
        timeAgoLabel.text = "5 mins ago"
        timeAgoLabel.textAlignment = .right
        container.addSubview(timeAgoLabel)
        
        moreLabel.font = AppFont.SFPro.medium(size: 14)
        moreLabel.textColor = UIColor(hex: 0xff2e00)
        moreLabel.text = "general.more".localized().uppercased()
        moreLabel.textAlignment = .right
        container.addSubview(moreLabel)
        
        //Constraints
        timeAgoLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().inset(14)
            make.right.equalToSuperview().inset(18)
        }
        
        infoLabel.snp.makeConstraints { (make) in
            make.left.equalTo(alertImageView.snp.right).offset(18)
            make.top.equalToSuperview().inset(12)
            make.bottom.equalToSuperview().inset(12)
            make.right.equalTo(timeAgoLabel.snp.left).offset(-18)
        }
        
        moreLabel.snp.makeConstraints { (make) in
            make.top.equalTo(timeAgoLabel.snp.bottom).offset(6)
            make.right.equalToSuperview().inset(18)
        }
    }
}
