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

import UIKit
import VDS

@objcMembers open class Line: VDS.Line, VDSMoleculeViewProtocol {
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    open var viewModel: LineModel!
    open var delegateObject: MVMCoreUIDelegateObject?
    open var additionalData: [AnyHashable : Any]?
    
    open override var orientation: Line.Orientation {
        didSet {
            if orientation == .horizontal {
                setContentHuggingPriority(.defaultLow, for: .horizontal)
                setContentHuggingPriority(.required, for: .vertical)
                setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
                setContentCompressionResistancePriority(.required, for: .vertical)
            } else {
                setContentHuggingPriority(.required, for: .horizontal)
                setContentHuggingPriority(.defaultLow, for: .vertical)
                setContentCompressionResistancePriority(.required, for: .horizontal)
                setContentCompressionResistancePriority(.defaultLow, for: .vertical)
            }
        }
    }

    //--------------------------------------------------
    // MARK: - Initializer
    //--------------------------------------------------    
    public convenience init(pinTo view: UIView, edge: UIRectEdge, useMargin: Bool) {
        self.init(frame: .zero)
        addLine(to: view, edge: edge, useMargin: useMargin)
    }
    
    public required init() {
        super.init()
        viewModel = LineModel(type: .secondary)
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        viewModel = LineModel(type: .secondary)
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        viewModel = LineModel(type: .secondary)
    }

    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    open func shouldBeVisible() -> Bool {
        guard let type = viewModel?.type else { return false }
        return type != .none
    }
    
    open func setStyle(_ style: LineModel.Style) {
        viewModel.type = style
        update(viewModel: viewModel)
    }
    
    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 draw(_ rect: CGRect) {
        guard viewModel.type != .none else { return }
        super.draw(rect)
    }
    
    //--------------------------------------------------
    // MARK: - VDSMoleculeViewProtocol
    //--------------------------------------------------
    open func viewModelDidUpdate() {
        surface = viewModel.surface
        style = VDS.Line.Style(rawValue: viewModel.type.rawValue) ?? .primary
        orientation = viewModel.orientation
    }
        
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    public func updateView(_ size: CGFloat) {
        setNeedsDisplay()
    }

    public static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 1
    }
}

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