/*
 * https://github.com/morethanwords/tweb
 * Copyright (C) 2019-2021 Eduard Kuzmenko
 * https://github.com/morethanwords/tweb/blob/master/LICENSE
 */

@import "../../scss/mixins";
@import "../../scss/variables";

$shadow: 0px 0 3px rgba(0, 0, 0, 0.20);
$color-inactive: rgba(255, 255, 255, .5);

.Viewer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 4;
  // pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
  visibility: visible;

  &.isInvisible {
    visibility: hidden;

    :global {
      .btn-send-container,
      .btn-send .animated-button-icon-icon {
        visibility: hidden !important;
      }
    }
  }

  &.isFull {
    .ViewerBackground {
      background-color: #000;
    }

    &.isReady {
      perspective: 1000px;
      transform-style: preserve-3d;
      overflow: hidden; // magic property
    }

    &.isReady .ViewerStoryContainer {
      --transition-time: .3s;
      --transition: var(--transition-standard-easing) var(--transition-time);
      visibility: hidden;
      opacity: 0;
      // height: 100%;
      margin: 0;
      transition-delay: 0s, 0s, var(--transition-time);

      @include animation-level-global(2) {
        transition: opacity var(--transition-time), transform var(--transition-time), visibility var(--transition-time);
      }

      &.fromLeft {
        transform: rotateY(90deg) translateX(50%) rotateY(-90deg) translateX(-50%) rotateY(-90deg);

        &.current {
          transform: rotateY(90deg) translateX(50%) rotateY(0deg) translateX(-50%) rotateY(-90deg);
        }
      }

      &.current {
        opacity: 1;
        visibility: visible;
        transition-delay: 0s, 0s, 0s;
      }

      &.fromRight {
        transform: rotateY(-90deg) translateX(-50%) rotateY(90deg) translateX(50%) rotateY(90deg);

        &.current {
          transform: rotateY(-90deg) translateX(-50%) rotateY(0deg) translateX(50%) rotateY(90deg);
        }
      }
    }

    :global(.stories-input) {
      --chat-input-padding: .5rem;
      --chat-input-background-color: #212121;
      --chat-input-focus-offset: 0px;
      --backdrop-filter: unset;
    }

    // .ViewerStory {
    //   height: 100%;
    //   // aspect-ratio: unset;
    // }

    // .ViewerStory,
    // .ViewerStoryContent,
    // .ViewerStoryContentMedia {
    //   border-radius: #{$border-radius-medium};
    // }

    // .ViewerStoryContent {
    //   aspect-ratio: inherit;
    //   overflow: hidden;
    //   top: 50%;
    //   transform: translateY(-50%);
    // }

    // :global(.stories-input) {
    //   bottom: .5rem;
    // }
  }

  &Background {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: rgba(0, 0, 0, .9);
    content: " ";
    pointer-events: none;

    :global(html.night) & {
      background-color: rgba(var(--body-background-color-rgb), .9);
    }
  }

  // &:not(.isVisible) &StoryContainer.small {
  //   opacity: 0;
  // }

  &Story {
    display: flex;
    flex-direction: column;
    width: var(--stories-width);
    aspect-ratio: 9 / 16;
    // max-width: var(--stories-width);
    border-radius: calc(#{$border-radius-medium} * (1 / var(--scale)));
    overflow: hidden;
    position: relative;
    z-index: 0;

    // shimmer
    &:before {
      z-index: 1;
    }

    @include animation-level-global(2) {
      transition: border-radius var(--transition-standard-out);
    }

    &Container {
      --translateX: 0;
      --scale: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding-bottom: 3.5rem;
      margin: .75rem 0;
      max-height: 100%;
      // * use 3d to fix blinking
      transform: translate3d(var(--translateX), 0, 0) scale3d(var(--scale), var(--scale), 1);
      will-change: transform, opacity;
      // opacity: 1;
      position: absolute;
      opacity: 1;
      // overflow: hidden;

      @include animation-level-global(2) {
        transition: transform var(--transition-standard-out), opacity var(--transition-standard-out);
      }

      &.fadeIn {
        opacity: 0;
      }

      &.small {
        --scale: 0.33;
        cursor: pointer;
      }

      .hideOnSmall {
        opacity: 1;
      }
  
      &.hold .hideOnSmall,
      &.small .hideOnSmall,
      &.small :global(.stories-input) {
        opacity: 0;
        pointer-events: none;
      }
    }

    &Container .hideOnSmall,
    &Container &ContentMediaContainer {
      @include animation-level-global(2) {
        transition: opacity var(--transition-standard-out) !important;
      }
    }

    &Container.small &ContentMediaContainer {
      opacity: .5;
    }

    &Container.small &Info {
      opacity: 1;

      @include animation-level-global(2) {
        transition: opacity var(--transition-standard-out);
      }
    }

    &Container.focused &ContentMediaContainer {
      opacity: .8;
    }

    &Slides {
      position: absolute;
      top: .625rem;
      left: 0;
      right: 0;
      padding: 0 .625rem 0 .4375rem;
      height: .125rem;
      display: flex;

      &Slide {
        border-radius: 1px;
        height: inherit;
        background-color: rgba(255, 255, 255, .3);
        margin-inline-start: .1875rem;
        flex: 1 1 auto;
        position: relative;
        min-width: .375rem;
        box-shadow: $shadow;

        &:before {
          position: absolute;
          top: 0;
          bottom: 0;
          width: var(--progress);
          border-radius: inherit;
          background-color: #fff;
          content: " ";
        }

        // &.active {
        //   background-color: rgba(255, 255, 255, 1);
        // }
      }
    }

    &Header {
      position: absolute;
      top: 1rem;
      left: 0;
      right: 0;
      padding: 0 .5rem;
      display: flex;
      align-items: center;
      justify-content: space-between;
      color: #fff;
      text-shadow: $shadow;

      &Avatar {
        width: 2rem;
        height: 2rem;
        margin-inline-end: .75rem;
        flex: 0 0 auto;
        box-shadow: $shadow;
      }

      &Name,
      &Time {
        @include text-overflow(true);
      }

      &Name {
        font-weight: var(--font-weight-bold);
      }

      &Secondary {
        font-size: var(--font-size-14);
        opacity: .5;
        flex: 0 0 auto;
      }

      &Time {
        flex: 1 1 auto;
        margin-top: -4px;
        height: 21px;

        &.hasRepost {
          display: flex;
          align-items: center;
          white-space: pre;
        }
      }

      &Left,
      &Right {
        display: flex;
        align-items: center;
      }

      &Left {
        padding: 0 .5rem;
        overflow: hidden;
        flex: 1 1 auto;
        height: 2.25rem;
        cursor: pointer;
      }

      &Right {
        flex: 0 0 auto;
      }

      &Info {
        flex: 1 1 auto;
        overflow: hidden;
      }

      &Row {
        max-width: 100%;
        overflow: hidden;
        display: flex;
        align-items: center;
        white-space: pre-wrap;
      }

      &Repost {
        display: inline-flex;
        align-items: center;
        gap: .25rem;
        // height: 0;
        font-size: var(--font-size-14);
        font-weight: var(--font-weight-bold);
        max-width: 100%;
        overflow: hidden;
        flex: 0 0 auto;

        &Title {
          @include text-overflow(true);
        }

        // &Icon {
        //   margin-inline-end: .25rem;
        // }
      }
    }

    &Privacy {
      border-radius: 50%;
      width: 1.5rem;
      height: 1.5rem;
      margin-inline-end: .5rem;
      cursor: pointer;
      color: #fff;
      font-size: 1rem;
      display: flex;
      align-items: center;
      justify-content: center;
      text-shadow: none;
    }

    &Content {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: 0;
      pointer-events: none;
      background-color: #000;

      &Item {
        position: relative;
        display: flex;
        align-items: center;
        width: 100%;
        height: 100%;
      }

      &Media {
        position: absolute;
        object-fit: cover;
        width: 100%;
        height: 100%;
      }
    }

    &Footer {
      display: flex;
      align-items: center;
      justify-content: space-between;
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      height: 3rem;
      color: $color-inactive;
      padding: 0 .5rem;
      font-weight: var(--font-weight-bold);

      @include animation-level-global(2) {
        transition: color var(--transition-standard-in);
      }

      @include hover-global() {
        color: rgba(255, 255, 255, 1);
      }

      &Left,
      &Right {
        display: flex;
        align-items: center;
      }

      &Icon {
        display: flex;
        align-items: center;

        &Icon {
          font-size: 1.5rem;
          margin-inline-end: .375rem;
        }
      }

      &Reaction {
        height: 2.5rem;
        padding: .5rem;
        cursor: pointer;
        border-radius: $border-radius;

        @include hover-global(gray, background-color, false);
      }

      &Reaction.isReacted &IconIcon {
        color: #FF3B30;
      }

      :global {
        .btn-icon {
          color: currentColor;
          transition: none;
        }

        .stacked-avatars {
          --border-color: #000;
          --margin-right: -1rem;
          --border-size: 2px;
          margin-inline-end: .5rem;
        }
      }

      &.isMe &Left {
        cursor: pointer;
      }

      &.isChangelog {
        justify-content: center;
        font-weight: var(--font-weight-normal);
      }
    }

    &Shadow {
      position: absolute;
      inset: 0;
      background: linear-gradient(180deg, rgba(0, 0, 0, 0.2) 0%, rgba(0, 0, 0, 0) 10.86%);
      pointer-events: none;

      &.hasCaption {
        background: linear-gradient(180deg, rgba(0, 0, 0, 0.2) 0%, rgba(0, 0, 0, 0) 11%, rgba(0, 0, 0, 0) 90%, rgba(0, 0, 0, 0.5) 100%);
      }
    }

    &Info {
      display: flex;
      flex-direction: column;
      align-items: center;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      opacity: 0;
      pointer-events: none;
      max-width: 100%;

      @include animation-level-global(2) {
        transition: opacity .1s var(--transition-standard-easing);
      }

      &Avatar {
        margin-bottom: 1.25rem;
        pointer-events: none !important;

        // :global(.avatar-stories-svg-circle:not(.is-unread)) {
        //   stroke: rgba(255, 255, 255, .3);
        // }
      }

      &Name {
        font-weight: var(--font-weight-bold);
        font-size: 3rem;
        color: #fff;
        text-align: center;
        line-height: var(--line-height);
        @include text-overflow(true);
        padding: 0 2rem;
        max-width: 100%;
        margin-bottom: -24px;
      }
    }

    &Caption {
      $shadow-size: .75rem;
      position: absolute;
      // top: 50%;
      // padding-top: 70%;
      top: calc(100% - 320px);
      padding-top: 264px;
      right: 0;
      bottom: 0;
      left: 0;
      height: auto;
      width: auto;
      padding-left: 1rem;
      // padding-right: 3.5rem;
      padding-right: 1rem;
      padding-bottom: .5rem;
      color: var(--white);
      font-size: var(--font-size-16);
      line-height: var(--line-height-16);
      white-space: pre-wrap;
      text-shadow: $shadow;
      z-index: 1;
      -webkit-mask-image: linear-gradient(
        to bottom,
        rgb(0, 0, 0, 0) 0%,
        rgb(0, 0, 0) #{$shadow-size},
        rgb(0, 0, 0) calc(100% - #{$shadow-size}),
        rgba(0, 0, 0, 0) 100%
      );
      // display: flex;
      // flex-direction: column;
      // gap: .25rem;
      // align-items: flex-start;

      a {
        color: #fff !important;
        text-decoration: underline;
      }

      &.hasReply {
        padding-top: 214px;
      }

      &Text {
        width: 100%;
        height: 100%;
        position: relative;
        display: table;
        word-break: break-word;
        white-space: pre-wrap;
        table-layout: fixed;
        // display: flex;
        // align-items: flex-end;

        &Cell {
          display: table-cell;
          vertical-align: bottom;
          user-select: text;
        }
      }
    }

    &MediaArea {
      position: absolute;
      transform: translate(-50%, -50%) rotate(var(--rotate));
      pointer-events: all;
      cursor: pointer;
      overflow: hidden;
      border-radius: 6px;
      z-index: 1;
      pointer-events: all;

      &Reaction {
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: unset;

        &Bubbles {
          position: absolute;
          inset: 0;
          background-color: #fff;
          border-radius: 50%;
          filter: drop-shadow(0 .125rem .25rem rgba(0, 0, 0, .1));
          pointer-events: none;

          &.dark {
            background-color: #000;
            opacity: .5;
          }
        }

        &Bubble {
          position: absolute;
          background-color: inherit;
          border-radius: 50%;
          width: 24%;
          height: 24%;
          top: 71%;
          left: 71%;

          &.small {
            width: 10%;
            height: 10%;
            top: 93%;
            left: 93%;
          }
        }

        &Inner {
          position: absolute;
          width: 72%;
          height: 72%;
          pointer-events: none;
          transform: scale(1);
          transform-origin: center 5%;

          @include animation-level-global(2) {
            transition: transform .15s ease-in-out;
          }

          &.hasCount {
            transform: scale(0.85);
          }

          :global(.media-sticker) {
            border-radius: .625rem;
          }
        }

        &Count {
          position: absolute;
          bottom: 0;
          font-weight: var(--font-weight-bold);
          color: #000;
          opacity: 0;

          @include animation-level-global(2) {
            transition: opacity .15s ease-in-out;
          }

          &.hasCount {
            opacity: 1;
          }

          &.dark {
            color: #fff;
          }
        }
      }
    }

    &MediaAreas {
      position: absolute;
      inset: 0;
      z-index: 1;
      opacity: 1;
      pointer-events: none;
    }

    &Reactions {
      --height: 3rem;
      --bubble-side-offset: 0px;
      --other-side-offset: 0px;
      position: absolute;
      bottom: .5rem;
      left: 50%;
      transform: translateX(-50%);
      background-color: rgba(0, 0, 0, .5) !important;
      backdrop-filter: blur(25px);
      opacity: 1 !important;
      visibility: visible !important;
      // max-width: 355px;
      // width: 355px;
      max-width: 319px;
      width: 319px;

      :global {
        .btn-menu-reactions {
          &-bubble {
            display: none;
          }

          &-reaction {
            --size: 2.25rem;
          }

          &-more {
            background-color: rgba(255, 255, 255, .1) !important;
            text-shadow: none;
            width: 1.875rem;
            height: 1.875rem;
            margin-inline: .375rem 0;
            font-size: 1.25rem;
          }
        }
      }
    }

    &Repost {
      margin-bottom: .25rem;
      width: fit-content;
      max-width: 100%;

      &Small {
        height: 1.5rem;
        line-height: 1.5rem;
        border-radius: 1rem;
        padding: 0 .5rem;
        background-color: rgba(0, 0, 0, .2);
        font-size: var(--font-size-14);
        font-weight: var(--font-weight-bold);
        cursor: pointer;

        @include text-overflow(true);
      }
    }

    :global {
      .btn-icon {
        color: #fff;
        text-shadow: $shadow;
      }
    }
  }

  :global {
    .stories-input {
      --chat-input-size: 3rem;
      --chat-input-padding: 0rem;
      --chat-input-border-radius: #{$border-radius-medium};
      --chat-input-focus-offset: 135px;
      --chat-input-max-width: calc(var(--stories-width) + var(--chat-input-focus-offset));
      --secondary-text-color: #707579;
      --primary-text-color: #fff;
      --focus-translateX: 0px;
      --chat-input-background-color: rgba(0, 0, 0, .5);
      --backdrop-filter: blur(25px);
      position: absolute;
      bottom: 0;
      width: var(--stories-width);
      max-width: var(--chat-input-max-width);
      opacity: 1;

      @include animation-level-global(2) {
        transition: opacity var(--transition-standard-out) !important;
      }

      // @include animation-level-global(2) {
      //   transition: width var(--transition-standard-out), opacity var(--transition-standard-out) !important;
      // }

      // &.is-focused {
      //   width: var(--chat-input-max-width);
      // }

      .rows-wrapper-wrapper {
        @include animation-level-global(2) {
          transition: transform var(--transition-standard-out) !important;
        }
      }

      .btn-send-container {
        opacity: 1;
        visibility: visible;

        @include animation-level-global(2) {
          transition: transform var(--transition-standard-out), opacity var(--transition-standard-out), visibility var(--transition-standard-out);
          transition-delay: 0s, 0s, 0s;
        }
      }

      .rows-wrapper-wrapper {
        transform: translateX(calc(var(--focus-translateX) * -1));
        transform-origin: center left;
      }

      .btn-send-container,
      .chat-input-secondary-button {
        transform: translateX(var(--focus-translateX));
        transform-origin: center right;
      }

      &.is-private {
        .btn-reaction {
          --add-translateX: calc(var(--chat-input-size) + #{$btn-send-margin});
        }

        &:not(.is-focused) .btn-send-container {
          opacity: 0;
          visibility: hidden;
          transition-delay: 0s, 0s, var(--transition-standard-out-time);
        }
      }

      &.is-focused {
        --focus-translateX: calc(var(--chat-input-focus-offset) / 2);

        .btn-reaction {
          opacity: 0;
          visibility: hidden;
          transition-delay: 0s, 0s, 0s, var(--transition-standard-out-time);
        }
      }

      .rows-wrapper {
        width: var(--stories-width);
        // max-width: calc(var(--chat-input-max-width) - var(--chat-input-size) - #{$btn-send-margin});
        max-width: calc(var(--chat-input-max-width) - (var(--chat-input-size) + #{$btn-send-margin} + var(--chat-input-padding)));

        @include animation-level-global(2) {
          transition: width var(--transition-standard-out) !important;
        }

        .btn-icon {
          margin-block: 0 2px;
        }

        .btn-icon.active,
        .attach-file.menu-open {
          color: #fff;
        }
      }

      .btn-reaction {
        --add-translateX: 0px;
        position: absolute;
        transform: translateX(calc(var(--focus-translateX) + var(--add-translateX)));
        inset-inline-end: calc(var(--chat-input-size) + var(--chat-input-padding) + #{$btn-send-margin});

        @include animation-level-global(2) {
          transition: .2s color, transform var(--transition-standard-out), opacity var(--transition-standard-out), visibility var(--transition-standard-out-time);
          transition-delay: 0s, 0s, 0s, 0s;
        }

        &:after {
          display: none;
        }

        &-default {
          color: #FF3B30 !important;
        }

        &-sticker {
          width: 26px;
          height: 26px;
          position: relative;
        }
      }

      // .btn-send:not(:hover):not(:active) {
      //   .btn-send-icon-forward {
      //     color: var(--secondary-text-color) !important;
      //   }
      // }

      .btn-send-icon-forward {
        color: var(--secondary-text-color);
      }

      .btn-send.forward,
      .btn-reaction {
        --color: var(--secondary-text-color);
        color: var(--color);
        background-color: var(--chat-input-background-color) !important;

        @include hover-global() {
          --color: var(--primary-text-color);
          color: var(--color) !important;
          background-color: var(--chat-input-background-color) !important;
        }
      }

      .btn-send {
        @include hover-global() {
          .btn-send-icon-forward {
            color: var(--primary-text-color) !important;
          }
        }

        @include animation-level-global(2) {
          transition: .2s color, .2s background-color;

          .btn-send-icon-forward {
            transition: .2s color;
          }
        }

        &:not(.forward) {
          .btn-send-icon-forward {
            color: var(--primary-text-color) !important;
          }
        }
      }

      .input-message-input {
        padding-inline: 0.375rem;
        caret-color: currentColor;
      }

      .new-message-wrapper {
        background-color: transparent;
      }

      .rows-wrapper:before,
      .bubble-tail,
      .chat-input-secondary-button,
      .emoji-dropdown {
        background-color: var(--chat-input-background-color) !important;
        backdrop-filter: var(--backdrop-filter);
      }

      .btn-send {
        backdrop-filter: var(--backdrop-filter);
      }

      // .bubble-tail {
      //   fill: rgba(0, 0, 0, .5);
      //   // backdrop-filter: blur(10px);
      // }

      .bubble-tail {
        clip-path: url(#message-tail-clip);
        transform: scale(-1);
        inset-inline-end: -7px;
        bottom: 0px;
      }

      .record-ripple {
        background: rgba(0, 0, 0, .25);
        backdrop-filter: blur(5px);
        opacity: 1;
      }
    }

    .emoji-dropdown {
      --translateX: 0px;
      left: auto;
      bottom: calc(var(--chat-input-size) + .5rem);
      border-radius: var(--chat-input-border-radius);
      transform: translateX(var(--translateX)) scale(0);

      &.active {
        transform: translateX(var(--translateX)) scale(1);
      }

      @media only screen and (max-height: 759px) {
        --translateX: -50%;
        left: 50%;
        transform-origin: bottom center;
      }
    }

    .autocomplete-helper {
      background-color: var(--chat-input-background-color) !important;
      backdrop-filter: var(--backdrop-filter);
    }

    .quiz-hint {
      z-index: 5;
    }

    .reply {
      --peer-color-rgb: 255, 255, 255;  
      display: block;
      background-color: rgba(0, 0, 0, .2);

      // &-title-icon {
      //   opacity: .5;
      // }
    }
  }

  &Close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    color: $color-inactive;
    transition: none;

    @include hover-global() {
      color: rgba(255, 255, 255, 1);
    }
  }

  .noSound {
    color: $color-inactive;
    // opacity: .5;
  }

  // &Slider {
  //   position: relative;
  //   display: flex;
  //   align-items: center;
  //   // justify-content: center;
  // }
}
