//
//  Line.swift
//  MVMCoreUI
//
//  Created by Scott Pfeil on 11/19/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import UIKit


@objcMembers open class Line: View {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    var lineModel: LineModel? {
        get { return model as? LineModel }
    }
    
    var lineBackgroundColor: Color? {
        return (lineModel?.inverted ?? false) ? lineModel?.backgroundColor_inverted : lineModel?.backgroundColor
    }
    
    //--------------------------------------------------
    // MARK: - Constraints
    //--------------------------------------------------
    
    public var heightConstraint: NSLayoutConstraint?
    public var widthConstraint: NSLayoutConstraint?
    
    open func updateLineConstraints(constant: CGFloat) {
        if let useVerticalLine = lineModel?.useVerticalLine, useVerticalLine {
            heightConstraint?.isActive = false
            widthConstraint?.isActive = true
            widthConstraint?.constant = constant
        } else {
            widthConstraint?.isActive = false
            heightConstraint?.isActive = true
            heightConstraint?.constant = constant
        }
    }
    
    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------
    
    public convenience init(pinTo view: UIView, edge: UIRectEdge, useMargin: Bool) {
        self.init(frame: .zero)
        addLine(to: view, edge: edge, useMargin: useMargin)
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    open func setStyle(_ style: LineModel.Style) {
        
        switch style {
        case .standard:
            updateLineConstraints(constant: 1)
            backgroundColor = lineModel?.backgroundColor?.uiColor ?? .mvmCoolGray3
        case .thin:
            updateLineConstraints(constant: 1)
            backgroundColor = lineModel?.backgroundColor?.uiColor ?? .mvmBlack
        case .medium:
            updateLineConstraints(constant: 2)
            backgroundColor = lineModel?.backgroundColor?.uiColor ?? .mvmBlack
        case .heavy:
            updateLineConstraints(constant: 4)
            backgroundColor = lineModel?.backgroundColor?.uiColor ?? .mvmBlack
        case .none:
            updateLineConstraints(constant: 0)
        }
    }
    
    open func shouldBeVisible() -> Bool {
        guard let type = lineModel?.type else { return false }
        return type != .none
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open func addLine(to view: UIView, edge: UIRectEdge, useMargin: Bool) {
        view.addSubview(self)
        NSLayoutConstraint.activate(Array(NSLayoutConstraint.pinView(toSuperview: self, useMargins: useMargin, pinTop: edge != .bottom, pinBottom: edge != .top, pinLeft: edge != .right, pinRight: edge != .left).values))
    }
    
    open override func setupView() {
        super.setupView()
        
        heightConstraint = heightAnchor.constraint(equalToConstant: 1)
        heightConstraint?.isActive = true
        widthConstraint = widthAnchor.constraint(equalToConstant: 1)
        widthConstraint?.isActive = false
        setStyle(.standard)
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        if let lineModel = model as? LineModel {
            setStyle(lineModel.type)
        }
    }
    
    open override func reset() {
        setStyle(.standard)
    }
    
    public override static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        
        guard let type = (model as? LineModel)?.type else { return 1 }
        
        switch type {
        case .none:
            return 0
        case .medium:
            return 2
        case .heavy:
            return 4
        default:
            return 1
        }
    }
}

extension Line: MVMCoreUIViewConstrainingProtocol {
    
    open func needsToBeConstrained() -> Bool {
        return true
    }
    
    open func copyBackgroundColor() -> Bool {
        return false
    }
}
