//
//  OrderTracker.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 7/1/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit
import MVMCoreUI

open class OrderTracker: View {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    var steps = [Step]()
    
    //--------------------------------------------------
    // MARK: - Life Cycle
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        isOpaque = false
    }
    
    open override func reset() {
        super.reset()
        
        resetSteps()
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    func constrain(stepModels: [StepModel]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
        
        resetSteps()
        
        guard let stepModels = stepModels else { return }
        
        var anchor = topAnchor
        
        for stepModel in stepModels {
            
            let step = Step()
            step.set(with: stepModel, delegateObject, additionalData)
            addSubview(step)
            steps.append(step)
            
            step.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
            trailingAnchor.constraint(equalTo: step.trailingAnchor).isActive = true
            step.topAnchor.constraint(equalTo: anchor).isActive = true
            
            anchor = step.bottomAnchor
        }
        
        bottomAnchor.constraint(equalTo: anchor).isActive = true
        (subviews.last as? Step)?.heightConstraint?.isActive = false
    }
    
    func resetSteps() {
        
        steps.forEach {
            $0.reset()
            $0.removeFromSuperview()
        }
        
        steps = []
    }
    
    //--------------------------------------------------
    // MARK: - Draw
    //--------------------------------------------------
    
    open override func draw(_ rect: CGRect) {
        
        guard let context = UIGraphicsGetCurrentContext(),
            let firstStep = steps.first
            else { return }
        
        context.setLineWidth(1)
        let imageDimension = firstStep.stateImage.bounds.height
        let halfDimension = imageDimension / 2
        let startPoint = CGPoint(x: halfDimension, y: halfDimension)
        let defaultGrey: UIColor = .mvmCoolGray3
        context.move(to: startPoint)
        
        var lineColor = (firstStep.state?.color() ?? defaultGrey).cgColor
        
        for (i, step) in steps.dropFirst().enumerated() {
            
            if let state = step.state, state == .incomplete {
                lineColor = defaultGrey.cgColor
            }
            
            // Allows server to override the line color for a custom color.
            if let customLineColor = step.stepModel?.lineColor?.cgColor {
                lineColor = customLineColor
            }
            
            context.setStrokeColor(lineColor)
            let relativeRect = convert(step.stateImage.frame, from: step.stateImage)
            let point = CGPoint(x: halfDimension, y: relativeRect.midY)
            
            context.addLine(to: point)
            context.strokePath()
            
            // Break out of loop since we're on the last step.
            if i == steps.count - 2 {
                break
            }
            
            lineColor = (step.state?.color() ?? defaultGrey).cgColor
            context.move(to: point)
        }
    }
    
    //------------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //------------------------------------------------------
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 196
    }
    
    public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = model as? OrderTrackerModel else { return }
        
        constrain(stepModels: model.steps, delegateObject: delegateObject, additionalData: additionalData)
        setNeedsLayout()
        setNeedsDisplay()
    }
}
