//
//  CheckboxLabel.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 9/13/19.
//  Copyright © 2019 Verizon Wireless. All rights reserved.
//


@objcMembers open class CheckboxLabel: View {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    public let checkbox = Checkbox()
    public let label = Label(fontStyle: .RegularBodySmall)
    
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public var checkboxPosition: CheckboxPosition = .center
    
    //--------------------------------------------------
    // MARK: - Constraints
    //--------------------------------------------------

    public var checkboxTopConstraint: NSLayoutConstraint?
    public var checkboxBottomConstraint: NSLayoutConstraint?
    public var checkboxCenterYConstraint: NSLayoutConstraint?
    
    //--------------------------------------------------
    // MARK: - Life Cycle
    //--------------------------------------------------
    
    override open func setupView() {
        super.setupView()
        
        guard subviews.isEmpty else { return }
        
        addSubview(checkbox)
        addSubview(label)
        
        label.text = ""
        
        checkbox.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
        
        checkboxBottomConstraint = layoutMarginsGuide.bottomAnchor.constraint(equalTo: checkbox.bottomAnchor)
        checkboxBottomConstraint?.isActive = true
        
        checkboxTopConstraint = checkbox.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor)
        checkboxTopConstraint?.isActive = true
        
        checkboxCenterYConstraint = checkbox.centerYAnchor.constraint(equalTo: centerYAnchor)
        
        label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true
        layoutMarginsGuide.trailingAnchor.constraint(equalTo: label.trailingAnchor).isActive = true
        label.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: PaddingTwo).isActive = true
        
        layoutMarginsGuide.bottomAnchor.constraint(greaterThanOrEqualTo: label.bottomAnchor).isActive = true
        let bottomLabelConstraint = layoutMarginsGuide.bottomAnchor.constraint(equalTo: label.bottomAnchor)
        bottomLabelConstraint.priority = .defaultLow
        bottomLabelConstraint.isActive = true
        
        alignCheckbox(.center)
    }
    
    @objc override open func updateView(_ size: CGFloat) {
        super.updateView(size)
        
        label.updateView(size)
        checkbox.updateView(size)
        layoutIfNeeded()
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    /// Aligns Checkbox and Label relative to the desired position of the Checkbox.
    private func alignCheckbox(_ position: CheckboxPosition) {
        checkboxPosition = position
        
        switch position {
        case .center:
            checkboxBottomConstraint?.isActive = false
            checkboxTopConstraint?.isActive = false
            checkboxCenterYConstraint?.isActive = true
            
        case .top:
            checkboxBottomConstraint?.isActive = false
            checkboxTopConstraint?.isActive = true
            checkboxCenterYConstraint?.isActive = false
            
        case .bottom:
            checkboxBottomConstraint?.isActive = true
            checkboxTopConstraint?.isActive = false
            checkboxCenterYConstraint?.isActive = false
        }
    }
    
    //--------------------------------------------------
    // MARK: - Atomic
    //--------------------------------------------------
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        guard let checkBoxWithLabelModel = model as? CheckboxLabelModel else { return  }

        if let checkboxAlignment = checkBoxWithLabelModel.checkboxAlignment {
            alignCheckbox(checkboxAlignment)
        }
        
        checkbox.set(with: checkBoxWithLabelModel.checkbox, delegateObject, additionalData)
        label.set(with: checkBoxWithLabelModel.label, delegateObject, additionalData)
    }
    
    open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        return 200
    }
    
    open override func reset() {
        super.reset()
        
        label.text = ""
        checkbox.reset()
        alignCheckbox(.center)
    }
}
