<!--
 Copyright (C) 2024 3NSoft Inc.

 This program is free software: you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software
 Foundation, either version 3 of the License, or (at your option) any later
 version.

 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 this program. If not, see <http://www.gnu.org/licenses/>.
-->

<script lang="ts" setup>
import { ref, onBeforeMount } from 'vue';

type DisplaySourceInfo = web3n.media.DisplaySourceInfo;

interface ScreenOption {
  id: string;
  name: string;
  display_id: string;
  thumbnailURL: string;
}

interface WindowOption {
  id: string;
  name: string;
  thumbnailURL: string;
  appIconURL?: string;
}

const screenChoices = ref<ScreenOption[]>();
const windowChoices = ref<WindowOption[]>();

// const phasesOfGatheringInfo;

async function displayChoicesAndReturnSelecton(
  choices: DisplaySourceInfo
): Promise<string|undefined> {

  // XXX
  screenChoices.value = choices.screens?.map(info => {
    const { id, name, display_id } = info;
    const thumbnailURL = toImgURL(info.thumbnail)!;
    return { id, name, display_id, thumbnailURL };
  });
  windowChoices.value = choices.windows?.map(info => {
    const { id, name } = info;
    const thumbnailURL = toImgURL(info.thumbnail)!;
    const appIconURL = toImgURL(info.appIcon);
    return { id, name, thumbnailURL, appIconURL };
  });

  // XXX DEBUG
  return;
}

function toImgURL(arr: Uint8Array|undefined): string|undefined {
  if (!arr) {
    return;
  }
  const blob = new Blob([ arr ], { type: 'image/bmp' });
  return URL.createObjectURL(blob);
}

onBeforeMount(async () => {
  await w3n.mediaDevices!.setSelectDisplayMediaForCaptureHandler!(
    displayChoicesAndReturnSelecton
  );

  // XXX
  navigator.mediaDevices.getDisplayMedia({ video: true, audio: true });

});

</script>

<template>
  <div>
  </div>
</template>

<style lang="scss" module>
.confirmationDialog {
  font-size: var(--font-14);
  font-weight: 400;
  color: var(--color-text-block-primary-default);
  text-align: center;
  padding: var(--spacing-l) var(--spacing-m);
}
</style>
