<!--
 Copyright (C) 2024 - 2025 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 { computed } from 'vue';
import { Ui3nButton } from '@v1nt1248/3nclient-lib';
import useCall from './useCall';
import IconButton from "@video/components/icon-button.vue";
import OwnVideo from "@video/components/own-video.vue";
import PeerVideo from "@video/components/peer-video.vue";
import PeerSharing from "@video/components/peer-sharing.vue";

const {
  user,
  isFullscreen,
  streams,
  peerName,
  peerVA,
  peerSharedStreams,
  peerVideoMuted,
  peerAudioMuted,
  toggleMicStatus,
  toggleCamStatus,
  endCall,
  toggleFullscreen,
  openScreenShareChoice,
} = useCall();

const layoutWithScreenShare = computed(
  () => (peerSharedStreams.value.length > 0)
);

</script>

<template>
  <section :class=$style.call>
    <div :class="[ $style.participants, {
      [$style.participantsWithSharing]: layoutWithScreenShare,
    }]">
      <peer-video :style-class=$style.participant
        :is-video-on="!peerVideoMuted"
        :is-audio-on="!peerAudioMuted"
        :stream=peerVA
        :peer=peerName
      />

      <own-video :class=$style.participant
        :is-cam-on="streams.isCamOn"
        :stream="streams.ownVA!.stream"
        :user="user"
      />
    </div>

    <div :class=$style.sharedScreens
      v-if=layoutWithScreenShare
    >
      <peer-sharing
        v-for="stream in peerSharedStreams"
        :stream=stream
      />
    </div>

    <div :class=$style.actions>
      <div :class=$style.actionGroup>
        <icon-button
          :icon="isFullscreen ? 'shrink' : 'expand-screen'"
          @click.stop.prevent="toggleFullscreen"
        />
      </div>

      <div :class=$style.actionGroup>
        <icon-button
          :icon="streams.isMicOn ? 'round-mic-none' : 'round-mic-off'"
          @click.stop.prevent="toggleMicStatus"
        />

        <icon-button
          :icon="streams.isCamOn ? 'outline-videocam' : 'outline-videocam-off'"
          @click.stop.prevent="toggleCamStatus"
        />

        <icon-button
          icon="outline-screen-share"
          @click.stop.prevent="openScreenShareChoice"
        />

        <icon-button
          icon="sharp-people"
          disabled
        />
      </div>

      <div :class=$style.actionGroup>
        <ui3n-button
          type="custom"
          color="var(--error-content-default)"
          icon="round-call-end"
          icon-color="var(--warning-fill-default)"
          @click.stop.prevent="endCall"
        >
          {{ $tr('va.end.call') }}
        </ui3n-button>
      </div>
    </div>
  </section>
</template>

<style lang="scss" module>
.call {
  --call-actions-height: 64px;
  --sharing-screens-height: 70%;

  position: relative;
  width: 100%;
  height: 100%;
}

.participants {
  position: relative;
  width: 100%;
  height: calc(100% - var(--call-actions-height));
  padding: 0 var(--spacing-m);
  display: flex;
  justify-content: center;
  align-items: center;
  column-gap: var(--spacing-m);
}

.participantsWithSharing {
  height: calc(
    100% - var(--call-actions-height) - var(--sharing-screens-height)
  );
  padding: 0 var(--spacing-m);
}

.participant {
  position: relative;
  width: calc(100% - var(--spacing-s));
  height: calc(100% - var(--spacing-s));
  display: flex;
  justify-content: center;
  align-items: center;
}

.sharedScreens {
  position: relative;
  width: 100%;
  height: var(--sharing-screens-height);
  padding: 0 var(--spacing-m);
}

.actions {
  position: relative;
  width: 100%;
  height: var(--call-actions-height);
  padding: 0 var(--spacing-m);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.actionGroup {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  column-gap: var(--spacing-l);
}

</style>
