@use '../core/tokens/token-utils';
@use './m3-datetimepicker';

$fallbacks: m3-datetimepicker.get-tokens();

$calendar-padding: 8px !default;

// We use the same padding as the month / year label, but subtract 16px since there is padding
// between the edge of the button and the text. This ensures that the button text lines up with
// the month / year label text.
$calendar-controls-side-margin: calc(33% / 7 - 16px);

// Values chosen to approximate https://material.io/icons/#ic_navigate_before and
// https://material.io/icons/#ic_navigate_next as closely as possible.
$calendar-prev-next-icon-border-width: 2px;
$calendar-prev-next-icon-margin: 15.5px;
$calendar-prev-icon-transform: translateX(2px) rotate(-45deg);
$calendar-next-icon-transform: translateX(-2px) rotate(45deg);

$landscape-calendar-header-width: 144px;

$panel-corner: token-utils.slot(datetimepicker-container-shape, $fallbacks);
$panel-divider: token-utils.slot(datetimepicker-calendar-header-divider-color, $fallbacks);

@mixin landscape-calendar-header {
  .mtx-calendar {
    display: flex;

    .mtx-calendar-header {
      width: $landscape-calendar-header-width;
      min-width: $landscape-calendar-header-width;
      padding: 16px 8px;
      border-bottom-width: 0;
      border-radius: $panel-corner 0 0 $panel-corner;
      border-right: 1px solid $panel-divider;

      [dir='rtl'] & {
        border-radius: 0 $panel-corner $panel-corner 0;
        border-right-width: 0;
        border-left: 1px solid $panel-divider;
      }
    }

    .mtx-calendar-header-year + .mtx-calendar-header-date-time,
    .mtx-calendar-header-date + .mtx-calendar-header-time {
      margin-top: 4px;
    }

    .mtx-calendar-header-date-time {
      font-size: 28px;
    }

    .mtx-calendar-header-time {
      display: flex;
      flex-direction: column;

      .mtx-calendar-header-hours,
      .mtx-calendar-header-minutes,
      .mtx-calendar-header-ampm {
        width: 40px;
        text-align: center;
      }
    }

    .mtx-calendar-header-ampm-container {
      flex-direction: row;
      font-size: 20px;
    }

    .mtx-calendar-header-ampm {
      padding: 4px;

      + .mtx-calendar-header-ampm {
        margin: 0 8px;
      }
    }
  }

  .mtx-datetimepicker-content-container-with-actions .mtx-calendar .mtx-calendar-header {
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
  }
}

@mixin landscape-actions-placeholder {
  .mtx-datetimepicker-actions::before {
    position: absolute;
    top: 0;
    left: 0;
    box-sizing: border-box;
    width: $landscape-calendar-header-width;
    height: 100%;
    content: '';
    border-right: 1px solid $panel-divider;
    background-color: token-utils.slot(datetimepicker-calendar-header-background-color, $fallbacks);
    border-bottom-left-radius: $panel-corner;

    [dir='rtl'] & {
      left: auto;
      right: 0;
      border-right-width: 0;
      border-left: 1px solid $panel-divider;
      border-bottom-left-radius: 0;
      border-bottom-right-radius: $panel-corner;
    }
  }
}

.mtx-calendar {
  display: block;
  outline: none;
  font-family: token-utils.slot(datetimepicker-calendar-text-font, $fallbacks);
  font-size: token-utils.slot(datetimepicker-calendar-text-size, $fallbacks);
}

.mtx-calendar-header {
  --mat-button-text-container-shape: #{token-utils.slot(datetimepicker-selector-container-shape, $fallbacks)};

  box-sizing: border-box;
  padding: 8px;
  border-bottom: 1px solid $panel-divider;
  border-top-left-radius: $panel-corner;
  border-top-right-radius: $panel-corner;
  background-color: token-utils.slot(datetimepicker-calendar-header-background-color, $fallbacks);
  color: token-utils.slot(datetimepicker-calendar-header-text-color, $fallbacks);

  .mtx-calendar-header-year,
  .mtx-calendar-header-date,
  .mtx-calendar-header-hours,
  .mtx-calendar-header-minutes,
  .mtx-calendar-header-ampm {
    height: auto;
    min-width: auto;
    padding: 0 4px;
    text-align: inherit;
    line-height: inherit;
    color: inherit;
    font-size: inherit;
    font-weight: inherit;
    letter-spacing: normal;
    white-space: normal;
    overflow-wrap: anywhere;

    .mat-mdc-button-touch-target {
      height: 100%;
    }
  }

  .mtx-calendar-header-year {
    line-height: 24px;
  }
}

.mtx-calendar-header-date-time {
  font-size: 24px;
  line-height: 36px;
}

.mtx-calendar-header-year,
.mtx-calendar-header-date,
.mtx-calendar-header-hours,
.mtx-calendar-header-minutes,
.mtx-calendar-header-ampm {
  &:not(.active) {
    opacity: 0.6;
  }

  &.not-clickable {
    cursor: initial;
  }
}

.mtx-calendar-header-time {
  display: inline-flex;

  &:not(.active) {
    opacity: 0.6;

    .mtx-calendar-header-hours,
    .mtx-calendar-header-minutes,
    .mtx-calendar-header-ampm {
      opacity: 1;
    }
  }
}

.mtx-calendar-header-hour-minute-separator {
  display: inline-block;
  width: 8px;
  text-align: center;
}

.mtx-calendar-header-ampm-container {
  display: inline-flex;
  flex-direction: column;
  line-height: 18px;
  font-size: 12px;
}

[mode='landscape'] {
  @include landscape-calendar-header;
  @include landscape-actions-placeholder;
}

@media all and (orientation: landscape) {
  [mode='auto'] {
    @include landscape-calendar-header;
    @include landscape-actions-placeholder;
  }
}

.mtx-calendar-content {
  width: 100%;
  padding: $calendar-padding;
  outline: none;
  box-sizing: border-box;
  overflow: hidden;
}

.mtx-calendar-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 $calendar-controls-side-margin;

  .mat-icon-button:hover .mat-button-focus-overlay {
    opacity: 0.04;
  }
}

.mtx-calendar-period-button {
  display: inline-block;
  height: 40px;
  line-height: 40px;
  outline: none;
  border: 0;
  background: transparent;
  box-sizing: border-box;
  font-size: token-utils.slot(datetimepicker-calendar-period-button-text-size, $fallbacks);
  font-weight: token-utils.slot(datetimepicker-calendar-period-button-text-weight, $fallbacks);
}

.mtx-calendar-previous-button,
.mtx-calendar-next-button {
  &.disabled {
    pointer-events: none;
    color: token-utils.slot(datetimepicker-calendar-date-disabled-state-text-color, $fallbacks);
  }

  svg {
    fill: currentColor;
    vertical-align: top;

    [dir='rtl'] & {
      transform: rotate(180deg);
    }
  }
}

.mtx-calendar-table {
  border-spacing: 0;
  border-collapse: collapse;
  width: 100%;
}

.mtx-calendar-table-header th {
  text-align: center;
  padding: $calendar-padding 0;
  color: token-utils.slot(datetimepicker-calendar-table-header-text-color, $fallbacks);
  font-size: token-utils.slot(datetimepicker-calendar-table-header-text-size, $fallbacks);
  font-weight: token-utils.slot(datetimepicker-calendar-table-header-text-weight, $fallbacks);
}
