//
//  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 }
    }
    
    //--------------------------------------------------
    // 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)
    }
    
    public init() {
        super.init(frame: .zero)
        model = LineModel(type: .secondary)
        setStyle(.secondary)
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        model = LineModel(type: .secondary)
        setStyle(.secondary)
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        model = LineModel(type: .secondary)
        setStyle(.secondary)
    }
    
    public required init(model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
        super.init(model: model, delegateObject, additionalData)
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    open func setStyle(_ style: LineModel.Style) {
        lineModel?.type = style
        backgroundColor = lineModel?.backgroundColor?.uiColor
        updateLineConstraints(constant: lineModel?.thickness ?? 1)
    }
    
    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
    }
    
    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(.secondary)
    }
    
    public override static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return (model as? LineModel)?.thickness ?? 1
    }
}

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