//
//  WifiWidgetModel.swift
//  MVMFrameworksSample
//
//  Created by Matt Bruce on 6/17/22.
//

import Foundation
import MVMCore
import MVMCoreUI

public class WifiWidgetModel: ContainerModel, MoleculeModelProtocol {
    //MoleculeModelProtocol
    public var backgroundColor: Color?
    public static var identifier: String { "wifiWidget" }
    public var moleculeName: String = WifiWidgetModel.identifier
    
    //UI Bound Properties
    public var wifiId: String
    public var title: String
    public var description: String
    public var password: String
    public var enabled: Bool = false
    public var editTitle: String
    public var shareTitle: String

    //Click Actions
    public var enabledAction: ActionModelProtocol
    public var editAction: ActionModelProtocol
    public var shareAction: ActionModelProtocol
    
    public init(wifiId: String, title: String, description: String, password: String, enabled: Bool, editTitle: String, shareTitle: String, enabledAction: ActionModelProtocol, editAction: ActionModelProtocol, shareAction: ActionModelProtocol,
        horizontalAlignment: UIStackView.Alignment? = nil, verticalAlignment: UIStackView.Alignment? = nil, useHorizontalMargins: Bool? = nil, leftPadding: CGFloat? = nil, rightPadding: CGFloat? = nil, useVerticalMargins: Bool? = nil, topPadding: CGFloat? = nil, bottomPadding: CGFloat? = nil) {
        
        self.wifiId = wifiId
        self.title = title
        self.description = description
        self.password = password
        self.enabled = enabled
        self.editTitle = editTitle
        self.shareTitle = shareTitle
        self.enabledAction = enabledAction
        self.editAction = editAction
        self.shareAction = shareAction
        super.init(horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment, useHorizontalMargins: useHorizontalMargins, leftPadding: leftPadding, rightPadding: rightPadding, useVerticalMargins: useVerticalMargins, topPadding: topPadding, bottomPadding: bottomPadding)
    }
    
    private enum CodingKeys: String, CodingKey {
        case moleculeName
        case backgroundColor
        case wifiId, title, description, password, enabled, editTitle, shareTitle
        case enabledAction, editAction, shareAction
    }

    required public init(from decoder: Decoder) throws {
        let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
        backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
        wifiId = try typeContainer.decode(String.self, forKey: .wifiId)
        title = try typeContainer.decode(String.self, forKey: .title)
        description = try typeContainer.decode(String.self, forKey: .description)
        password = try typeContainer.decode(String.self, forKey: .password)
        enabled = try typeContainer.decode(Bool.self, forKey: .enabled)
        editTitle = try typeContainer.decode(String.self, forKey: .editTitle)
        shareTitle = try typeContainer.decode(String.self, forKey: .shareTitle)
        enabledAction = try typeContainer.decodeModel(codingKey: .enabledAction)
        editAction = try typeContainer.decodeModel(codingKey: .editAction)
        shareAction = try typeContainer.decodeModel(codingKey: .shareAction)
        try super.init(from: decoder)
    }

    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
        try container.encode(moleculeName, forKey: .moleculeName)
        try container.encode(wifiId, forKey: .wifiId)
        try container.encode(title, forKey: .title)
        try container.encode(description, forKey: .description)
        try container.encode(password, forKey: .password)
        try container.encode(enabled, forKey: .enabled)
        try container.encode(editTitle, forKey: .editTitle)
        try container.encode(shareTitle, forKey: .shareTitle)
        try container.encodeModel(enabledAction, forKey: .enabledAction)
        try container.encodeModel(editAction, forKey: .editAction)
        try container.encodeModel(shareAction, forKey: .shareAction)
    }

}

extension WifiWidgetModel {
    
    public var titleModel: TestLabelToggleModel {
        let labelModel = LabelModel(text: title)
        let toggleModel = TestToggleModel(enabled)
        toggleModel.action = enabledAction
        toggleModel.showText = true
        return TestLabelToggleModel(labelModel, toggleModel)
    }
    
    public var passwordModel: TextEntryFieldModel {
        let model = TextEntryFieldModel(with: password)
        model.fieldKey = "preferredLastName"
        model.type = .password
        model.readOnly = true
        return model
    }

    public var descriptionModel: LabelModel { LabelModel(text: description) }
    public var editModel: LinkModel { LinkModel(title: editTitle, action: editAction) }
    public var shareModel: LinkModel { LinkModel(title: shareTitle, action: shareAction) }

}



