//
//  AutoLayoutImageObserverView.swift
//
//  Created by Pierre Molinaro on 05/02/2021.
//
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//  THIS FILE IS GENERATED BY EASY BINDINGS, DO NOT MODIFY IT
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

import Cocoa

//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//   AutoLayoutImageObserverView
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

final class AutoLayoutImageObserverView : NSImageView, EBUserClassNameProtocol {

  //····················································································································

  private let mSize : EBControlSize

  //····················································································································

  init (size inSize : EBControlSize) {
    self.mSize = inSize
    super.init (frame: NSRect ())
    noteObjectAllocation (self)
    self.translatesAutoresizingMaskIntoConstraints = false
    self.imageScaling = .scaleProportionallyUpOrDown
    self.imageFrameStyle = .none
    self.controlSize = inSize.cocoaControlSize
    self.font = NSFont.systemFont (ofSize: NSFont.systemFontSize (for: self.controlSize))
    self.frame.size = self.intrinsicContentSize
  }

  //····················································································································

  required init? (coder : NSCoder) {
    fatalError ("init(coder:) has not been implemented")
  }

  //····················································································································

  deinit {
    noteObjectDeallocation (self)
  }

  //····················································································································

  override func ebCleanUp () {
    self.mImageController?.unregister ()
    self.mImageController = nil
    self.mTooltipController?.unregister ()
    self.mTooltipController = nil
    super.ebCleanUp ()
  }

  //····················································································································

  override var intrinsicContentSize : NSSize {
    let s : CGFloat
    switch self.mSize {
    case .mini : s = 15.0 // ???
    case .small : s = 17.0
    case .regular : s =  24.0
    }
    return NSSize (width: s, height: s)
  }

  //····················································································································
  //  image binding
  //····················································································································

  fileprivate func updateImage (_ object : EBReadOnlyProperty_NSImage) {
    switch object.selection {
    case .empty :
      self.image = nil
      self.enable (fromValueBinding: false)
    case .multiple :
      self.image = nil
      self.enable (fromValueBinding: false)
    case .single (let propertyValue) :
      self.image = propertyValue
      self.enable (fromValueBinding: true)
    }
  }

  //····················································································································

  fileprivate var mImageController : EBReadOnlyPropertyController? = nil

  //····················································································································

  final func bind_image (_ object : EBReadOnlyProperty_NSImage) -> Self {
    self.mImageController = EBReadOnlyPropertyController (
      observedObjects: [object],
      callBack: { [weak self] in self?.updateImage (object) ; }
    )
    return self
  }

  //····················································································································
  //  tooltip binding
  //····················································································································

  fileprivate func updateTooltip (_ object : EBReadOnlyProperty_String) {
    switch object.selection {
    case .empty :
      self.toolTip = nil
      self.enable (fromValueBinding: false)
    case .multiple :
      self.toolTip = nil
      self.enable (fromValueBinding: false)
    case .single (let propertyValue) :
      self.toolTip = propertyValue
      self.enable (fromValueBinding: true)
    }
  }

  //····················································································································

  private var mTooltipController : EBReadOnlyPropertyController? = nil

  //····················································································································

  final func bind_tooltip (_ object : EBReadOnlyProperty_String) -> Self {
    self.mTooltipController = EBReadOnlyPropertyController (
      observedObjects: [object],
      callBack: { [weak self] in self?.updateTooltip (object) }
    )
    return self
  }

  //····················································································································

}

//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
