//
//  TypedMessageView.swift
//  MessageCenter
//
//  Created by Jeytery on 13.05.2021.
//

import Foundation

import UIKit

class TypedMessageView: UIView {
 
    enum EventSessionViewColorTheme {
        
        case red
        case green
        case white
        case gray
        
        var backbroundColor: UIColor {
            switch self {
            case .red:
                return .systemRed
            case .green:
                return .systemGreen
            case .white:
                return .white
            case .gray:
                return .darkGray
            }
        }
        
        var textColor: UIColor {
            switch self {
            case .red:
                return .white
            case .green:
                return .white
            case .white:
                return .darkGray
            case .gray:
                return .white
            }
        }
    }
    
    private var boldText = ""
    
    private var thinText: String = ""
    
    private var colorTheme: EventSessionViewColorTheme
    
    private lazy var stackView = UIStackView(arrangedSubviews: [boldLabel, thinLabel])
    
    private var boldLabel: UILabel = {
        let l = UILabel()
        l.font = .boldSystemFont(ofSize: 20.0)
        l.numberOfLines = 1
        return l
    }()
    
    private var thinLabel: UILabel = {
        let l = UILabel()
        l.font = .systemFont(ofSize: 16)
        l.numberOfLines = 2
        return l
    }()
    
    private func appleColorTheme(theme: EventSessionViewColorTheme) {
        backgroundColor = theme.backbroundColor
        boldLabel.textColor = theme.textColor
        thinLabel.textColor = theme.textColor
    }
    
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        layer.cornerRadius = 5
        addSubview(stackView)
        boldLabel.text = boldText
        thinLabel.text = thinText
        stackView.axis = .vertical
        stackView.distribution = .fillEqually
        stackView.spacing = 4
        
        if boldText == "" {
            boldLabel.removeFromSuperview()
        }
        
        if thinText == "" {
            thinLabel.removeFromSuperview()
        }
        
        appleColorTheme(theme: colorTheme)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let stackViewHeight = bounds.height/2
        stackView.frame = CGRect(
            x: 10,
            y: bounds.height/2 - stackViewHeight/2,
            width: bounds.width-20,
            height: bounds.height/2)
    }
    
    public init(
        boldTitle: String = "",
        thinTitle: String = "",
        colorTheme: EventSessionViewColorTheme = .gray,
        frame: CGRect)
    {
        self.boldText = boldTitle
        self.thinText = thinTitle
        self.colorTheme = colorTheme
        super.init(frame: frame)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
