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

import Foundation


open class CarouselIndicatorModel: CarouselPagingModelProtocol, MoleculeModelProtocol, EnableableModelProtocol {
    //--------------------------------------------------
    // MARK: - Properties
    //--------------------------------------------------
    
    public class var identifier: String {
        return ""
    }
    
    public var backgroundColor: Color?
    public var moleculeName: String?
    public var numberOfPages: Int = 0
    
    /// Sets the current Index to focus on.
    public var currentIndex: Int = 0
    public var animated: Bool = true
    public var hidesForSinglePage: Bool = false
    public var inverted: Bool = false
    /// Set true to make the accessibility value as "Slide #currentPage of #totalPage", otherwise will be "Page #currentPage of #totalPage", default is false
    public var accessibilityHasSlidesInsteadOfPage: Bool = false
    public var enabled: Bool = true
    public var disabledIndicatorColor: Color = Color(uiColor: .mvmCoolGray3)
    public var indicatorColor: Color = Color(uiColor: .mvmBlack)
    public var indicatorColor_inverted: Color = Color(uiColor: .mvmWhite)
    public var position: CGFloat?
    
    /// Allows sendActions() to trigger even if index is already at min/max index.
    public var alwaysSendAction = false
    
    //--------------------------------------------------
    // MARK: - Keys
    //--------------------------------------------------
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case currentIndex
        case numberOfPages
        case alwaysSendAction
        case animated
        case hidesForSinglePage
        case accessibilityHasSlidesInsteadOfPage
        case enabled
        case disabledIndicatorColor
        case indicatorColor
        case position
        case inverted
    }
    
    //--------------------------------------------------
    // MARK: - Codec
    //--------------------------------------------------
    public init() {}
    
    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        moleculeName = try typeContainer.decodeIfPresent(String.self, forKey: .moleculeName)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        
        if let currentIndex = try typeContainer.decodeIfPresent(Int.self, forKey: .currentIndex) {
            self.currentIndex = currentIndex
        }
        
        if let alwaysSendAction = try typeContainer.decodeIfPresent(Bool.self, forKey: .alwaysSendAction) {
            self.alwaysSendAction = alwaysSendAction
        }
        
        if let inverted = try typeContainer.decodeIfPresent(Bool.self, forKey: .inverted) {
            self.inverted = inverted
        }
        
        if let position = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .position) {
            self.position = position
        }
        
        if let animated = try typeContainer.decodeIfPresent(Bool.self, forKey: .animated) {
            self.animated = animated
        }
        
        if let hidesForSinglePage = try typeContainer.decodeIfPresent(Bool.self, forKey: .hidesForSinglePage) {
            self.hidesForSinglePage = hidesForSinglePage
        }
        
        if let accessibilityHasSlidesInsteadOfPage = try typeContainer.decodeIfPresent(Bool.self, forKey: .accessibilityHasSlidesInsteadOfPage) {
            self.accessibilityHasSlidesInsteadOfPage = accessibilityHasSlidesInsteadOfPage
        }
        
        if let enabled = try typeContainer.decodeIfPresent(Bool.self, forKey: .enabled) {
            self.enabled = enabled
        }
        
        if let disabledIndicatorColor = try typeContainer.decodeIfPresent(Color.self, forKey: .disabledIndicatorColor) {
            self.disabledIndicatorColor = disabledIndicatorColor
        }
        
        if let indicatorColor = try typeContainer.decodeIfPresent(Color.self, forKey: .indicatorColor) {
            self.indicatorColor = indicatorColor
        }
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(moleculeName, forKey: .moleculeName)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encode(numberOfPages, forKey: .numberOfPages)
        try container.encode(currentIndex, forKey: .currentIndex)
        try container.encode(alwaysSendAction, forKey: .alwaysSendAction)
        try container.encode(animated, forKey: .animated)
        try container.encode(hidesForSinglePage, forKey: .hidesForSinglePage)
        try container.encode(accessibilityHasSlidesInsteadOfPage, forKey: .accessibilityHasSlidesInsteadOfPage)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(inverted, forKey: .inverted)
        try container.encode(disabledIndicatorColor, forKey: .disabledIndicatorColor)
        try container.encode(indicatorColor, forKey: .indicatorColor)
        try container.encodeIfPresent(position, forKey: .position)
    }
}
