// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
extern "js" fn set_viewport_height(
  elem : @dom.HTMLCanvasElement,
  height : Double,
) -> Unit = "(x, y) => { x.height = y; }"

///|
extern "js" fn set_viewport_width(
  elem : @dom.HTMLCanvasElement,
  width : Double,
) -> Unit = "(x, y) => { x.width = y; }"

///|
extern "js" fn request_pointer_lock(elem : @dom.HTMLCanvasElement) -> Unit = "(elem) => elem.requestPointerLock();"

///|
extern "js" fn is_pointer_locked(canvas : @dom.HTMLCanvasElement) -> Bool = "(canvas) => document.pointerLockElement === canvas"

///|
extern "js" fn document_add_event_listener(
  event : String,
  callback : (@dom.Event) -> Unit,
) -> Unit = "(event, callback) => { document.addEventListener(event, callback); }"

///|
extern "js" fn set_image_smoothing_enabled(
  ctx : @dom.CanvasRenderingContext2D,
  value : Bool,
) -> Unit = "(ctx, value) => ctx.imageSmoothingEnabled = value;"

///|
extern "js" fn set_text_align(
  ctx : @dom.CanvasRenderingContext2D,
  value : String,
) -> Unit = "(ctx, value) => ctx.textAlign = value;"

///|
extern "js" fn set_text_baseline(
  ctx : @dom.CanvasRenderingContext2D,
  value : String,
) -> Unit = "(ctx, value) => ctx.textBaseline = value;"

///|
extern "js" fn load_font_async(font : String, path : String) -> Unit =
  #| (font, path) => {
  #|   const ff = new FontFace(font, 'url(' + path + ')');
  #|   ff.load().then(loadedFont => {
  #|     document.fonts.add(loadedFont);
  #|   })
  #| }

///|
extern "js" fn begin_path(ctx : @dom.CanvasRenderingContext2D) -> Unit = "(ctx) => ctx.beginPath();"

///|
pub extern "js" fn Audio::new(path : String) -> Audio = "(path) => new Audio(path)"

///|
pub extern "js" fn Audio::play(self : Self) -> Unit = "(self) => self.play()"

///|
pub extern "js" fn Audio::pause(self : Self) -> Unit = "(self) => self.pause()"

///|
pub extern "js" fn Audio::set_volume(self : Self, volume : Double) -> Unit = "(self, volume) => self.volume = volume"

///|
pub extern "js" fn Audio::set_loop(self : Self, loop_ : Bool) -> Unit = "(self, loop_) => self.loop = loop_"

///|
#external
type Audio