//
//  AutoLayoutAngleCircularSlider.swift
//  ElCanari
//
//  Created by Pierre Molinaro on 15/06/2021.
//
//  THIS FILE IS GENERATED BY EASY BINDINGS, DO NOT MODIFY IT
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

import Cocoa

//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//   AutoLayoutAngleCircularSlider
//  Min Value : 0, max Value : 3600
//  Current value :    0 -> knob at top
//                   900 -> knob at right
//                  1800 -> knob at bottom
//                  2700 -> knob at left
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

final class AutoLayoutAngleCircularSlider : AutoLayoutBase_NSSlider {

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

  init (size inSize : EBControlSize) {
    super.init (min: 0, max: 3600, ticks: 360)

    self.controlSize = inSize.cocoaControlSize
    self.sliderType = .circular
 //   self.doubleValue = 1800.0
  }

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

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

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

  override func sendAction (_ action : Selector?, to : Any?) -> Bool {
    let value = (450_000 - Int ((self.doubleValue * 100.0).rounded ())) % 360_000
    _ = self.mAngleController?.updateModel (
      withCandidateValue: value,
      windowForSheet: self.window
    )
    let r = super.sendAction (action, to: to)
    flushOutletEvents ()
    return r
  }

  //····················································································································
  //  doubleValue binding
  //····················································································································

  fileprivate func updateDoubleValue (_ object : EBReadOnlyProperty_Double) {
    switch object.selection {
    case .empty, .multiple :
      self.stringValue = "-"
      self.enable (fromValueBinding: false, self.enabledBindingController)
    case .single (let propertyValue) :
      self.doubleValue = propertyValue
      self.enable (fromValueBinding: true, self.enabledBindingController)
    }
  }

  //····················································································································
  //  intValue binding
  //····················································································································

  fileprivate func updateAngleValue (_ object : EBReadOnlyProperty_Int) {
    switch object.selection {
    case .empty, .multiple :
      self.enable (fromValueBinding: false, self.enabledBindingController)
      self.doubleValue = 0.0
    case .single (let propertyValue) :
      self.doubleValue = Double ((90_000 + 360_000 - propertyValue) % 360_000) / 100.0
      self.enable (fromValueBinding: true, self.enabledBindingController)
    }
  }

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

  private var mAngleController : EBGenericReadWritePropertyController <Int>? = nil

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

  final func bind_angle (_ inObject : EBReadWriteProperty_Int, sendContinously : Bool) -> Self {
    self.mAngleController = EBGenericReadWritePropertyController <Int> (
      observedObject: inObject,
      callBack: { [weak self] in self?.updateAngleValue (inObject) }
    )
    self.isContinuous = sendContinously
    return self
  }

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

}

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