//
//  DocumentControlView.swift
//  App
//
//  Created by James Magahern on 9/30/20.
//

import UIKit

class DocumentControlItemView: UIControl
{
    static public let controlHeight = CGFloat(48.0)
    
    let imageView = UIImageView(frame: .zero)
    let label = UILabel(frame: .zero)
    
    enum ViewType: String {
        case imageView
        case label
        case separator
        case highlightView
    }
    
    var drawsBottomSeparator: Bool = false {
        didSet { setNeedsLayout() }
    }
    
    internal let highlightView = UIView(frame: .zero)
    internal let separatorView = UIView(frame: .zero)
    
    init() {
        super.init(frame: .zero)
        
        addSubview(highlightView)
        addSubview(imageView)
        addSubview(label)
        addSubview(separatorView)
        
        tintColor = .label
        
        label.font = UIFont.preferredFont(forTextStyle: .body)
        label.textAlignment = .left
        
        imageView.contentMode = .center
        
        separatorView.backgroundColor = .secondarySystemFill
        highlightView.backgroundColor = .secondarySystemFill
        
        highlightView.isHidden = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        CGSize(width: size.width, height: Self.controlHeight)
    }
    
    private func box(_ bounds: CGRect) -> Box<ViewType> {
        Box {
            let padding: CGFloat = 18.0
            let imageSize: CGFloat = 24.0
            let ibounds = bounds.inset(by: layoutMargins)
            
            let imageRect = CGRect(
                x: ibounds.minX + 6.0, y: 0.0,
                width: imageSize, height: imageSize
            ).centeredY(inRect: bounds)
            
            (ViewType.imageView, imageRect)
            
            (ViewType.highlightView, bounds)
            
            (ViewType.label, CGRect(
                x: imageRect.maxX + padding, y: ibounds.minY,
                width: ibounds.width - imageRect.maxX - padding, height: ibounds.height
            ))
            
            let separatorHeight: CGFloat = 1.0
            
            if drawsBottomSeparator {
                (ViewType.separator, CGRect(
                    x: bounds.minX, y: bounds.height - separatorHeight,
                    width: bounds.width, height: separatorHeight
                ))
            } else {
                (ViewType.separator, CGRect.zero)
            }
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        box(bounds).fill([
            .imageView     : imageView,
            .separator     : separatorView,
            .label         : label,
            .highlightView : highlightView
        ])
        
        separatorView.isHidden = !drawsBottomSeparator
    }
    
    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
        super.beginTracking(touch, with: event)
        highlightView.isHidden = false
        
        return true
    }
    
    override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
        super.endTracking(touch, with: event)
        highlightView.isHidden = true
    }
    
    public func title(_ title: String) -> Self {
        self.label.text = title
        return self
    }
    
    public func image(_ image: UIImage?) -> Self {
        self.imageView.image = image
        return self
    }
    
    public func symbol(_ name: String) -> Self {
        return self.image(UIImage(systemName: name))
    }
}
