@use 'sass:map';
@use '~@angular/material' as mat;

@mixin color($theme, $columnPalette, $rowPalette) {
  $config: mat.get-color-config($theme);
  $primary: map.get($config, primary);
  $accent: map.get($config, accent);
  $background: map.get($config, background);
  $foreground: map.get($config, foreground);
  $is-dark-theme: map.get($config, is-dark);

  rds-c-tabulation-table {
    table[mat-table] {
      background-color: mat.get-color-from-palette($background, background);
    }
  }
  rds-c-tabulation-table.mat-primary {
    @include _rds-c-tabulation-table-palette($primary, $accent, $columnPalette, $rowPalette, $foreground, $background, $is-dark-theme);
  }
  rds-c-tabulation-table.mat-accent {
    @include _rds-c-tabulation-table-palette($accent, $primary, $columnPalette, $rowPalette, $foreground, $background, $is-dark-theme);
  }
}

@mixin typography($theme) {
}

@mixin theme($theme, $columnPalette, $rowPalette) {
  $config: mat.get-color-config($theme);
  @if $config != null {
    @include color($theme, $columnPalette, $rowPalette);
  }

  $typography-config: mat.get-typography-config($theme);
  @if $typography-config != null {
    @include typography($theme);
  }
}

@mixin _rds-c-tabulation-table-palette($primary, $accent, $columnPalette, $rowPalette, $foreground, $background, $is-dark-theme) {
  $backgroundColor: mat.get-color-from-palette($background, background);
  $columnColor: mat.get-color-from-palette($columnPalette, default);
  $columnContrastColor: mat.get-color-from-palette($columnPalette, default-contrast);
  $primaryTextColor: mat.get-color-from-palette($foreground, text);
  $rowAltColor: if($is-dark-theme, mat.get-color-from-palette($rowPalette, 900), mat.get-color-from-palette($rowPalette, 50));
  $rowAltContrastColor: if(
    $is-dark-theme,
    mat.get-contrast-color-from-palette($rowPalette, 900),
    mat.get-contrast-color-from-palette($rowPalette, 50)
  );
  $rowColor: mat.get-color-from-palette($rowPalette, default);
  $rowContrastColor: mat.get-color-from-palette($rowPalette, default-contrast);
  table[mat-table] {
    tr {
      &.even {
        td:not(.total-col) {
          &.count-cell,
          &.row-header-cell,
          &.last-row-header-cell {
            background-color: $rowAltColor;
          }
          &.count-cell,
          &.row-header-cell {
            color: if($is-dark-theme, $rowAltContrastColor, $primaryTextColor);
          }
          &.count-cell.sub-total-cell,
          &.last-row-header-cell {
            color: $rowAltContrastColor;
          }
        }
      }
      &.sub-total {
        td {
          &.count-cell,
          &.last-row-header-cell {
            color: $rowContrastColor;
          }
        }
      }
      &.row-names-column-values-row {
        th:not(.row-dimension-header):not(.total-col) {
          border-bottom-color: $columnColor;
        }
      }
      &.column-names-row {
        th.col-dimension-header {
          background-color: $columnColor;
          color: $columnContrastColor;
        }
      }
      th {
        &.mat-header-cell {
          color: $primaryTextColor;
          &.total-col {
            @include _totalHeaderCell($accent);
          }
          &.sub-total-cell {
            color: $primaryTextColor;
          }
        }
        &.row-dimension-header {
          background-color: $rowColor;
          color: $rowContrastColor;
          &:not(:first-child)::before {
            content: '';
            display: block;
            position: absolute;
            bottom: 0;
            top: 0;
            left: 0;
            width: 1px;
            background-color: $backgroundColor;
          }
        }

        @include _firstCol($columnColor);
        @include _evenCol($columnColor, $columnContrastColor, $primaryTextColor, $is-dark-theme);
      }
      td {
        @include _firstCol($columnColor);
        @include _evenCol($columnColor, $columnContrastColor, $primaryTextColor, $is-dark-theme);
        &.row-header-cell {
          color: $primaryTextColor;
          &.mat-footer-cell.row-header-cell {
            @include _totalHeaderCell($accent);
          }
        }
        &.mat-cell {
          border-bottom-color: $rowColor;
          &.last-row-header-cell {
            color: $primaryTextColor;
          }
        }
      }
    }
    .total-col {
      background-color: if(
        $is-dark-theme,
        mat.get-color-from-palette($accent, default, 0.38),
        mat.get-color-from-palette($accent, default, 0.12)
      );
    }
    tr.sub-total .total-col,
    .row-header-cell.mat-footer-cell,
    .mat-footer-cell {
      background-color: if(
        $is-dark-theme,
        mat.get-color-from-palette($accent, default, 0.38),
        mat.get-color-from-palette($accent, default, 0.12)
      );
      color: mat.get-contrast-color-from-palette($accent, default);
    }
  }
}

@mixin _evenCol($backgroundColor, $backgroundContrastColor, $primaryTextColor, $is-dark-theme) {
  &.even-col {
    color: if($is-dark-theme, $backgroundContrastColor, $primaryTextColor);
    &.sub-total-cell,
    &.mat-footer-cell {
      color: if($is-dark-theme, #fff, #000);
    }
    &::after {
      background-color: if($is-dark-theme, rgba($backgroundColor, 0.38), rgba($backgroundColor, 0.12));
    }
  }
}

@mixin _firstCol($borderColor) {
  &.first-col {
    border-left: 1px solid $borderColor;
  }
  &.last-col {
    border-right: 1px solid $borderColor;
  }
}

@mixin _totalHeaderCell($themeColor) {
  background-color: mat.get-color-from-palette($themeColor, default);
  color: mat.get-color-from-palette($themeColor, default-contrast);
}
