//
// Copyright (c) 2018 Muukii <muukii.app@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation

import PixelEngine


open class HighlightsControlBase : FilterControlBase {

  public required init(context: PixelEditContext) {
    super.init(context: context)
  }
}

open class HighlightsControl : HighlightsControlBase {

  open override var title: String {
    return L10n.editHighlights
  }

  private let navigationView = NavigationView()

  public let slider = StepSlider(frame: .zero)

  open override func setup() {
    super.setup()

    backgroundColor = Style.default.control.backgroundColor

    TempCode.layout(navigationView: navigationView, slider: slider, in: self)

    slider.mode = .plus
    slider.addTarget(self, action: #selector(valueChanged), for: .valueChanged)

    navigationView.didTapCancelButton = { [weak self] in

      self?.context.action(.revert)
      self?.pop(animated: true)
    }

    navigationView.didTapDoneButton = { [weak self] in

      self?.context.action(.commit)
      self?.pop(animated: true)
    }
  }

  open override func didReceiveCurrentEdit(_ edit: EditingStack.Edit) {

    slider.set(value: edit.filters.highlights?.value ?? 0, in: FilterHighlights.range)

  }

  @objc
  private func valueChanged() {

    let value = slider.transition(in: FilterHighlights.range)

    guard value != 0 else {
      context.action(.setFilter({ $0.highlights = nil }))
      return
    }

    var f = FilterHighlights()
    f.value = value
    context.action(.setFilter({ $0.highlights = f }))
  }

}
