//
//  ForecastPrecipitationCell.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 18.03.2021.
//

import UIKit

class ForecastPrecipitationCell: UITableViewCell {
    //Private
    private let titleLabel = UILabel()
    private let stackView = UIStackView()
    private let infoContainer = UIView()
    private let infoImageView = UIImageView()
    private let infoLabel = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareCell()
        prepareTitle()
        prepareStackView()
        prepareInfoContainer()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK:- Prepare
private extension ForecastPrecipitationCell {
    func prepareCell() {
        selectionStyle = .none
        contentView.backgroundColor = ThemeManager.currentTheme.baseBackgroundColor
    }
    
    func prepareTitle() {
        titleLabel.font = AppFont.SFPro.bold(size: 16)
        titleLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        titleLabel.text = "precipitation.title".localized().uppercased()
        contentView.addSubview(titleLabel)
    }
    
    func prepareStackView() {
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.alignment = .center
        stackView.spacing = 0
        stackView.clipsToBounds = false
        stackView.isLayoutMarginsRelativeArrangement = true
        stackView.layoutMargins = .init(top: 0, left: 6, bottom: 0, right: 6)
        contentView.addSubview(stackView)
        
        stackView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(18)
            make.left.right.equalToSuperview().inset(18)
        }
    }
    
    func prepareInfoContainer() {
        infoImageView.contentMode = .scaleAspectFit
        infoImageView.image = UIImage(named: "humidity")
        infoContainer.addSubview(infoImageView)
        
        infoImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().inset(20)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(12)
        }
        
        infoLabel.font = AppFont.SFPro.regular(size: 13)
        infoLabel.textColor = ThemeManager.currentTheme.secondaryTextColor
        infoLabel.text = "Possible light shower between 1 PM - 2 PM"
        infoContainer.addSubview(infoLabel)
        
        infoLabel.snp.makeConstraints { (make) in
            make.left.equalTo(infoImageView.snp.right).offset(8)
            make.right.equalToSuperview().inset(8)
            make.centerY.equalToSuperview()
        }
        
        infoContainer.backgroundColor = UIColor(hex: 0xd9ebfe)
        infoContainer.layer.cornerRadius = 12
        contentView.addSubview(infoContainer)
        
        infoContainer.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(18)
            make.height.equalTo(40)
            make.top.equalTo(stackView.snp.bottom).offset(20).priority(.medium)
            make.bottom.equalToSuperview().inset(15)
        }
    }
}
