//
//  DoughnutChartView.swift
//  MVMCoreUI
//
//  Created by Murugan, Vimal on 26/12/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//

import Foundation


@objcMembers open class DoughnutChartView: View {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var doughnutChart = DoughnutChart()
    var colorLablesStack = ColorViewLabelsStack()
    
    public var doughnutChartModel: DoughnutChartModel? {
        get { return model as? DoughnutChartModel }
    }
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        
        addSubview(doughnutChart)
        addSubview(colorLablesStack)
        
        doughnutChart.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        doughnutChart.topAnchor.constraint(greaterThanOrEqualTo: topAnchor).isActive = true
        
        bottomAnchor.constraint(greaterThanOrEqualTo: doughnutChart.bottomAnchor).isActive = true
        
        let doughnutBottomAnchor = bottomAnchor.constraint(equalTo: doughnutChart.bottomAnchor)
        doughnutBottomAnchor.priority = UILayoutPriority(rawValue: 200)
        doughnutBottomAnchor.isActive = true
        
        bottomAnchor.constraint(greaterThanOrEqualTo: colorLablesStack.bottomAnchor).isActive = true
        
        let colorLablesBottomAnchor = bottomAnchor.constraint(equalTo: colorLablesStack.bottomAnchor)
        colorLablesBottomAnchor.priority = UILayoutPriority(rawValue: 204)
        colorLablesBottomAnchor.isActive = true
        
        colorLablesStack.topAnchor.constraint(greaterThanOrEqualTo: topAnchor).isActive = true
        
        let colorLablesTopAnchor = colorLablesStack.topAnchor.constraint(equalTo: topAnchor)
        colorLablesTopAnchor.priority = .defaultLow
        colorLablesTopAnchor.isActive = true
        
        trailingAnchor.constraint(equalTo: colorLablesStack.trailingAnchor).isActive = true
        doughnutChart.centerYAnchor.constraint(equalTo: colorLablesStack.centerYAnchor).isActive = true

        colorLablesStack.leadingAnchor.constraint(equalTo: doughnutChart.trailingAnchor, constant: PaddingThree).isActive = true
        colorLablesStack.backgroundColor = .clear
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        doughnutChart.updateView(size)
        colorLablesStack.updateView(size)
    }
    
    open override func reset() {
        super.reset()
        doughnutChart.reset()
        colorLablesStack.reset()
    }
    
    //--------------------------------------------------
    // MARK: - MoleculeViewProtocol
    //--------------------------------------------------
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        
        guard let model = doughnutChartModel else { return }
        doughnutChart.set(with: model, delegateObject, additionalData)
        
        // Create the stack model
        var stackItems: [MoleculeStackItemModel] = []
        for item in model.sections {
            stackItems.append(MoleculeStackItemModel(with: item))
        }
        let stack = StackModel(molecules: stackItems)
        stack.verticalAlignment = .fill
        colorLablesStack.set(with: stack, delegateObject, additionalData)
    }
}

// MARK: - MVMCoreUIViewConstrainingProtocol
extension DoughnutChartView: MVMCoreUIViewConstrainingProtocol {
    
    open func horizontalAlignment() -> UIStackView.Alignment {
        return .leading
    }
}

class ColorViewLabelsStack: MoleculeStackView {
    
    override func createStackItemsFromModel(_ model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        guard let stackItemModels = stackModel?.molecules else { return }
        for model in stackItemModels {
            let view = ColorViewWithLabel()
            let stackItem = MoleculeStackItem(andContain: view)
            stackItem.set(with: model, delegateObject, additionalData)
            stackItems.append(stackItem)
        }
    }
}
