//
//  NumericIndicatorView.swift
//  MVMCoreUI
//
//  Created by Kevin Christiano on 2/3/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//

import UIKit


open class NumericIndicatorView: CarouselIndicator {
    //--------------------------------------------------
    // MARK: - Outlets
    //--------------------------------------------------
    
    /// Text to display the current count of total pages for viewing.
    open var pageCount: Label = {
        let label = Label(fontStyle: .RegularBodySmall)
        label.isAccessibilityElement = false
        label.setContentCompressionResistancePriority(.required, for: .vertical)
        label.textAlignment = .center
        return label
    }()
    
    let leftArrow: Arrow = {
        let arrow = Arrow(model: ArrowModel(), nil, nil)
        arrow.isAccessibilityElement = false
        arrow.direction = .left
        arrow.pinHeightAndWidth()
        return arrow
    }()
    
    let rightArrow: Arrow = {
        let arrow = Arrow(model: ArrowModel(), nil, nil)
        arrow.pinHeightAndWidth()
        return arrow
    }()
    
    //--------------------------------------------------
    // MARK: - Computed Properties
    //--------------------------------------------------
    
    open override var isEnabled: Bool {
        didSet { setViewColor(isEnabled ? indicatorColor : disabledIndicatorColor) }
    }
        
    /// Sets the color for pageCount text, left arrow and right arrow.
    public override var indicatorColor: UIColor {
        get { super.indicatorColor }
        set (newColor) {
            super.indicatorColor = newColor
            
            if isEnabled {
                setViewColor(newColor)
            }
        }
    }
    
    //--------------------------------------------------
    // MARK: - Lifecycle
    //--------------------------------------------------
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
        pageCount.updateView(size)
    }
    
    //--------------------------------------------------
    // MARK: - Setup
    //--------------------------------------------------
    
    open override func setupView() {
        super.setupView()
        
        accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "swipe_to_select_with_action_hint")
        addSubview(pageCount)
        addSubview(leftArrow)
        addSubview(rightArrow)
        
        NSLayoutConstraint.activate([
            pageCount.centerXAnchor.constraint(equalTo: centerXAnchor),
            pageCount.topAnchor.constraint(equalTo: topAnchor),
            bottomAnchor.constraint(equalTo: pageCount.bottomAnchor),
            leftArrow.centerYAnchor.constraint(equalTo: centerYAnchor),
            rightArrow.centerYAnchor.constraint(equalTo: centerYAnchor),
            leftArrow.leadingAnchor.constraint(equalTo: leadingAnchor),
            pageCount.leadingAnchor.constraint(equalTo: leftArrow.trailingAnchor, constant: Padding.Two),
            rightArrow.leadingAnchor.constraint(equalTo: pageCount.trailingAnchor, constant: Padding.Two),
            trailingAnchor.constraint(equalTo: rightArrow.trailingAnchor)
        ])
    }
    
    //--------------------------------------------------
    // MARK: - Methods
    //--------------------------------------------------
    
    public override func assessTouchOf(_ touchPoint_X: CGFloat) {
        
        if touchPoint_X > bounds.width / 2 {
            incrementCurrentIndex()
        } else {
            decrementCurrentIndex()
        }
    }
    
    private func setViewColor(_ newColor: UIColor) {
        
        pageCount.textColor = newColor
        leftArrow.color = newColor
        rightArrow.color = newColor
        rightArrow.setNeedsDisplay()
        leftArrow.setNeedsDisplay()
    }

    //--------------------------------------------------
    // MARK: - IndicatorViewProtocol
    //--------------------------------------------------
    
    open override func updateUI(previousIndex oldIndex: Int, newIndex: Int, totalCount: Int, isAnimated: Bool) {
        
        pageCount.text = "\(newIndex + 1)/\(totalCount)"
        formatAccessibilityValue(index: newIndex + 1, total: totalCount)
        layoutIfNeeded()
    }
}
