// Copyright (C) 2023 The Android Open Source Project
//
// 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.

@import "../theme";

@mixin filled-color-scheme($intent-color, $text-color) {
  color: $text-color;
  background: $intent-color;

  &:hover {
    background: color_hover($intent-color);
  }

  &:active,
  &.pf-active {
    background: color_active($intent-color);
  }

  &[disabled] {
    &:hover {
      background: $intent-color;
    }

    &.pf-active {
      background: color_active($intent-color);
    }
  }
}

@keyframes pf-spinner-rotation {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(360deg);
  }
}

.pf-button {
  font-family: var(--pf-font-compact);
  line-height: 1;
  user-select: none;
  border-radius: $border-radius;
  padding: 4px 8px;
  white-space: nowrap;
  min-width: max-content;
  position: relative;

  &--loading {
    // The button is unclickable when loading
    pointer-events: none;

    .pf-button__label,
    .pf-left-icon,
    .pf-right-icon {
      visibility: hidden;
    }

    &::after {
      content: "";
      height: 0.9em;
      width: 0.9em;
      border-radius: 100px;
      border-style: solid;
      border-width: 0.15em;
      border-color: var(--pf-color-border);
      border-top-color: var(--pf-color-primary);
      position: absolute;
      inset: 50%;
      translate: -50% -50%;
      animation: pf-spinner-rotation 1s infinite linear;
    }
  }

  &--shrink {
    min-width: 0;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  &--rounded {
    border-radius: 100px;
  }

  & > .pf-left-icon {
    margin-right: 6px; // Make some room between the icon and label
  }

  & > .pf-right-icon {
    margin-left: 6px; // Make some room between the icon and label
  }

  &__tooltip {
    font-size: var(--pf-font-size-s);
  }

  &:focus-visible {
    @include focus;
  }

  // Reduce padding when compact
  &.pf-compact {
    padding: 2px 4px;
    & > .pf-left-icon {
      margin-right: 2px;
    }

    & > .pf-right-icon {
      margin-left: 2px;
    }
  }

  // Reduce padding when we are icon-only
  &.pf-icon-only {
    & > .pf-left-icon {
      margin: 0;
    }

    padding: 4px;

    &.pf-compact {
      padding: 0;
    }

    &::before {
      // Inserting a zero-width space to keep the button in flow. This allows it
      // to stay aligned with the baseline of other text around it.
      content: "\200B"; /* Zero-width space */
    }
  }

  &[disabled] {
    filter: opacity(50%);
    cursor: not-allowed;
  }

  // From here on in we just describe the colors
  &--filled {
    background: color-mix(
      in srgb,
      transparent,
      var(--pf-color-interactive-base) 10%
    );

    &:hover {
      background: color-mix(
        in srgb,
        transparent,
        var(--pf-color-interactive-base) 15%
      );
    }

    &.pf-active,
    &:active {
      background: color-mix(
        in srgb,
        transparent,
        var(--pf-color-interactive-base) 20%
      );
    }

    &[disabled] {
      &:hover {
        background: color-mix(
          in srgb,
          transparent,
          var(--pf-color-interactive-base) 10%
        ); // Reset to the default background
      }

      &.pf-active {
        background: color-mix(
          in srgb,
          transparent,
          var(--pf-color-interactive-base) 40%
        );
      }
    }

    &.pf-intent-primary {
      @include filled-color-scheme(
        var(--pf-color-primary),
        var(--pf-color-text-on-primary)
      );
    }

    &.pf-intent-success {
      @include filled-color-scheme(
        var(--pf-color-success),
        var(--pf-color-text-on-success)
      );
    }

    &.pf-intent-danger {
      @include filled-color-scheme(
        var(--pf-color-danger),
        var(--pf-color-text-on-danger)
      );
    }

    &.pf-intent-warning {
      @include filled-color-scheme(
        var(--pf-color-warning),
        var(--pf-color-text-on-warning)
      );
    }
  }

  &--outlined {
    border: solid 1px var(--pf-color-border);
  }

  &--minimal,
  &--outlined {
    &:hover {
      background: color_hover(transparent);
    }

    &:active,
    &.pf-active {
      background: color_active(transparent);
    }

    &.pf-intent-primary {
      color: var(--pf-color-primary);
    }

    &.pf-intent-success {
      color: var(--pf-color-success);
    }

    &.pf-intent-danger {
      color: var(--pf-color-danger);
    }

    &.pf-intent-warning {
      color: var(--pf-color-warning);
    }

    &[disabled] {
      &:hover {
        background: unset;
      }

      &.pf-active {
        background: color_active(transparent);
      }
    }
  }
}

.pf-button-bar {
  display: flex;
  flex-direction: row;
  gap: 2px;
  align-items: baseline;
}

.pf-button-group {
  display: flex;
  flex-direction: row;
  align-items: baseline;

  .pf-button:not(:last-child) {
    border-top-right-radius: unset;
    border-bottom-right-radius: unset;
    border-right: unset;
  }

  .pf-button:not(:first-child) {
    border-top-left-radius: unset;
    border-bottom-left-radius: unset;
  }
}
