//
//  ButtonWithImage.swift
//  MobileFirstFramework
//
//  Created by Bandaru, Krishna Kishore on 23/02/21.
//  Copyright © 2021 Verizon Wireless. All rights reserved.
//

import UIKit
import MVMCoreUI

public class ButtonWithImage: PillButton {
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)

        guard let model = model as? ButtonWithImageModel, var buttonImage = MVMCoreCache.shared()?.getImageFromRegisteredBundles(model.image) else {
            return
        }
        
        if let tintColor = model.tintColor {
            buttonImage = buttonImage.withRenderingMode(.alwaysTemplate)
            self.tintColor = tintColor.uiColor
        }
        
        setImage(buttonImage, for: .normal)
        semanticContentAttribute = model.imagePosition == .left ? .forceLeftToRight : .forceRightToLeft
        updateContentInsets(with: Padding.OneHalf)
    }
    
    //Adding space between image and text & content to be centered in container
    public func updateContentInsets(with insetAmount: CGFloat) {
        
        guard let imagePosition = (model as? ButtonWithImageModel)?.imagePosition else { return }
        
        imageEdgeInsets = UIEdgeInsets(top: 0, left: imagePosition == .left ? -insetAmount: insetAmount, bottom: 0, right: imagePosition == .left ? insetAmount : -insetAmount)
        titleEdgeInsets = UIEdgeInsets(top: 0, left: imagePosition == .left ? insetAmount : -insetAmount, bottom: 0, right: imagePosition == .left ? -insetAmount : insetAmount)
        contentEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: insetAmount)
    }
}



//
//  ButtonWithImageModel.swift
//  MobileFirstFramework
//
//  Created by Bandaru, Krishna Kishore on 23/02/21.
//  Copyright © 2021 Verizon Wireless. All rights reserved.
//


public class ButtonWithImageModel: ButtonModel {
    
    public enum ImagePosition: String, Codable {
        case left, right
    }
    
    public override class var identifier: String {
        "buttonWithImage"
    }
    
    let image: String
    var imagePosition: ImagePosition
    var tintColor: Color?
    
    public init(with title: String, image: String, imagePosition: ImagePosition = .left, action: ActionModelProtocol) {
        self.image = image
        self.imagePosition = imagePosition
        super.init(with: title, action: action)
    }
    
    public init(secondaryButtonWith title: String, image: String, imagePosition: ImagePosition = .left, action: ActionModelProtocol) {
        self.image = image
        self.imagePosition = imagePosition
        super.init(secondaryButtonWith: title, action: action)
    }
    
    public init(primaryButtonWith title: String, image: String, imagePosition: ImagePosition = .left, action: ActionModelProtocol) {
        self.image = image
        self.imagePosition = imagePosition
        super.init(primaryButtonWith: title, action: action)
    }
    
    private enum CodingKeys: String, CodingKey {
        case image
        case imagePosition
        case tintColor
    }
    
    required public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        image = try container.decode(String.self, forKey: .image)
        imagePosition = try container.decodeIfPresent(ImagePosition.self, forKey: .imagePosition) ?? .left
        tintColor = try container.decodeIfPresent(Color.self, forKey: .tintColor)
        try super.init(from: decoder)
    }
    
    public override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(image, forKey: .image)
        try container.encode(imagePosition, forKey: .imagePosition)
        try container.encodeIfPresent(tintColor, forKey: .tintColor)
        try super.encode(to: encoder)
    }
}

public class neViewModl: ListItemModel, MoleculeModelProtocol {
    public static var identifier: String = "fios"
}

public class neView: TableViewCell {
    public let voicemailReadView: View = {
        let view = View()
        view.backgroundColor = .mvmRed
        view.heightAnchor.constraint(equalToConstant: 10).isActive = true
        view.widthAnchor.constraint(equalToConstant: 10).isActive = true
        view.layer.cornerRadius = 5
        return view
    }()
    
    public let mdn: Label = {
        let label = Label(fontStyle: .RegularBodySmall)
        label.setContentCompressionResistancePriority(.required, for: .horizontal)
        return label
    }()
    
    public let contact: Label = {
        let label = Label(fontStyle: .BoldBodySmall)
//        label.setContentCompressionResistancePriority(.required, for: .horizontal)
//        label.setContentCompressionResistancePriority(.required, for: .vertical)
//        label.setContentHuggingPriority(.required, for: .horizontal)
        return label
    }()
    
    public let shortDate: Label = {
        let label = Label(fontStyle: .RegularBodySmall)
        label.setContentCompressionResistancePriority(.required, for: .horizontal)
        label.textAlignment = .right
        return label
    }()
    
    public let fullDate: Label = {
        let label = Label(fontStyle: .RegularBodySmall)
        label.setContentCompressionResistancePriority(.required, for: .horizontal)
        return label
    }()
    
    public let duration: Label = {
        let label = Label(fontStyle: .RegularBodySmall)
        label.setContentCompressionResistancePriority(.required, for: .horizontal)
        label.textAlignment = .right
        return label
    }()
    
    public let callDetailsButton: ImageView = {
        let button = ImageView()
        button.heightAnchor.constraint(equalToConstant: 20).isActive = true
        button.widthAnchor.constraint(equalToConstant: 20).isActive = true
        button.contentMode = .scaleAspectFit
        button.backgroundColor = .blue
        return button
    }()
        
    public let callBackButton: PillButton = {
        let button = PillButton()
        return button
    }()
    
    public let sendTextButton: PillButton = {
        let button = PillButton()
        return button
    }()
//
//    public lazy var leftLabelStack: UIStackView = {
//        let stack = UIStackView(arrangedSubviews: [contact, mdn])
//        stack.translatesAutoresizingMaskIntoConstraints = false
//        stack.axis = .vertical
//        stack.alignment = .leading
//        stack.distribution = .fill
//        return stack
//    }()
    
//    public lazy var row1Stack: UIStackView = {
//        let stack = UIStackView(arrangedSubviews: [voicemailReadView, contact, shortDate, callDetailsButton])
//            stack.translatesAutoresizingMaskIntoConstraints = false
//            stack.axis = .horizontal
//            stack.alignment = .center
//            stack.distribution = .fill
//            return stack
//    }()
    
    public lazy var row1Stack: Stack<StackModel> = {
        Stack<StackModel>.createStack(with: [(view: voicemailReadView, model: StackItemModel()),
                                             (view: contact, model: StackItemModel(horizontalAlignment: .leading)),
                                             (view: shortDate, model: StackItemModel()),
                                             (view: callDetailsButton, model: StackItemModel())],
                                                           axis: .horizontal, spacing: PaddingOne)
    }()

//    public lazy var row2Stack: UIStackView = {
//        let stack = UIStackView(arrangedSubviews: [mdn, duration])
//            stack.translatesAutoresizingMaskIntoConstraints = false
//            stack.axis = .horizontal
//            stack.alignment = .center
//            stack.distribution = .fill
//            return stack
//    }()
    public lazy var row2Stack: Stack<StackModel> = {
        Stack<StackModel>.createStack(with: [(view: mdn, model: StackItemModel(horizontalAlignment: .leading)),
                                             (view: duration, model: StackItemModel())],
                                                           axis: .horizontal)
    }()
    
//    public lazy var leftTopStack: Stack<StackModel> = {
//        Stack<StackModel>.createStack(with: [(view: voicemailReadView, model: StackItemModel()),
//                                             (view: contact, model: StackItemModel())],
//                                                           axis: .horizontal)
//    }()
    
//    public lazy var leftStack: UIStackView = {
//        let stack = UIStackView(arrangedSubviews: [leftTopStack, mdn])
//        stack.translatesAutoresizingMaskIntoConstraints = false
//        stack.axis = .vertical
//        stack.alignment = .fill
//        return stack
//    }()
    
//    public lazy var leftStack: Stack<StackModel> = {
//        Stack<StackModel>.createStack(with: [(view: leftTopStack, model: StackItemModel(horizontalAlignment: .leading)),
//                                             (view: mdn, model: StackItemModel(horizontalAlignment: .leading))],
//                                                           axis: .vertical)
//    }()
    
//    public lazy var rightStack: UIStackView = {
//       let stack = UIStackView(arrangedSubviews: [rightLabelStack, callDetailsButton])
//        stack.translatesAutoresizingMaskIntoConstraints = false
//       stack.axis = .horizontal
//       stack.alignment = .trailing
//       return stack
//    }()
//
//    public lazy var rightStack: Stack<StackModel> = {
//        Stack<StackModel>.createStack(with: [(view: rightLabelStack, model: StackItemModel(verticalAlignment: .leading)),
//                                             (view: callDetailsButton, model: StackItemModel(verticalAlignment: .leading))],
//                                                           axis: .horizontal)
//    }()
    
//    public lazy var rightLabelStack: UIStackView = {
//       let stack = UIStackView(arrangedSubviews: [shortDate, duration])
//        stack.translatesAutoresizingMaskIntoConstraints = false
//       stack.axis = .vertical
//       stack.alignment = .fill
//       return stack
//    }()
//
//    public lazy var rightLabelStack: Stack<StackModel> = {
//        Stack<StackModel>.createStack(with: [(view: shortDate, model: StackItemModel()),
//                                             (view: duration, model: StackItemModel())],
//                                                           axis: .vertical)
//    }()
    
//    public lazy var topStack: UIStackView = {
//        let stack = UIStackView(arrangedSubviews: [row1Stack, row2Stack])
//        stack.translatesAutoresizingMaskIntoConstraints = false
//        stack.axis = .vertical
//        stack.alignment = .fill
//        stack.distribution = .fill
//        return stack
//    }()
    public lazy var topStack: Stack<StackModel> = {
        Stack<StackModel>.createStack(with: [(view: row1Stack, model: StackItemModel()),
                                             (view: row2Stack, model: StackItemModel())],
                                                           axis: .vertical, spacing: 0)
    }()
//
//    public lazy var topStack: Stack<StackModel> = {
//        Stack<StackModel>.createStack(with: [(view: row1Stack, model: StackItemModel(verticalAlignment: .leading)),
//                                             (view: row2Stack, model: StackItemModel(verticalAlignment: .leading))],
//                                                           axis: .horizontal)
//    }()
    
//    public lazy var mainStack: UIStackView = {
//        let stack = UIStackView(arrangedSubviews: [leftHeadline, leftBody])
//        stack.axis = .vertical
//        stack.alignment = .leading
//        return stack
//    }()
    
    open override func setupView() {
        super.setupView()
        
        addMolecule(topStack)
        row1Stack.restack()
        row2Stack.restack()
        topStack.restack()
    }
    
    open override func updateView(_ size: CGFloat) {
        super.updateView(size)
//        leftLabelStack.updateView(size)
        topStack.updateView(size)
    }
    
    open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
        super.set(with: model, delegateObject, additionalData)
        let mdnModel: LabelModel? = LabelModel(text: "555-555-5555")
        let contactModel: LabelModel? = LabelModel(text: "Unknown caller")
        contactModel?.fontStyle = .BoldBodySmall
        let shortDateModel: LabelModel? = LabelModel(text: "5/15/2020")
        shortDateModel?.textAlignment = .right
        let durationModel: LabelModel? = LabelModel(text: "34 sec")
        durationModel?.textAlignment = .right
        
//        voicemailReadView.isHidden = false
        contact.setOptional(with: contactModel, delegateObject, additionalData)
        mdn.setOptional(with: mdnModel, delegateObject, additionalData)
        shortDate.setOptional(with: shortDateModel, delegateObject, additionalData)
        duration.setOptional(with: durationModel, delegateObject, additionalData)

//        fullDate.setOptional(with: nil, delegateObject, additionalData)
//        audioPlayer.setOptional(with: model.audioPlayer, delegateObject, additionalData)
//        callBackButton.setOptional(with: model.callBackButton, delegateObject, additionalData)
//        sendTextButton.setOptional(with: model.sendTextButton, delegateObject, additionalData)
//        callDetailsButton.setOptional(with: model.callDetailsImageButton, delegateObject, additionalData)
//        voiceMailModel?.audioPlayer.messageID = model.messageID
//
//        if let isHeard = model.isHeard, !isHeard, let messageNumber = model.messageID, !MFUtility.checkIfVoicemailExists(messageNumber){
//            changeVoicemailReadStatus(isHeard: false)
//        } else {
//            changeVoicemailReadStatus(isHeard: true)
//        }
        
    }
    
    public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
        200
    }
}
