/*
 * Copyright (c) 2008-2016 Haulmont.
 *
 * 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.
 *
 */

/**
 * The font weight of table header.
 * @group table
 */
$cuba-table-header-font-weight: $v-font-weight !default;
/**
 * Defines whether table row should be colored when it is hovered.
 * @group table
 */
$cuba-table-highlight-hover-row: false !default;

/**
 * Defines default background color of table total aggregation row. Calculated automatically.
 * @group table
 */
$cuba-table-aggregation-background-color-default: $cuba-aggregation-background-color-default;

/**
 * Defines background color of table total aggregation row.
 * @group table
 */
$cuba-table-aggregation-background-color: $cuba-table-aggregation-background-color-default !default;

/**
 * Border color of table when it is drop target.
 */
$c-table-drag-indicator-color: $v-focus-color;

@mixin halo-table($primary-stylename: v-table) {
  $background-color: $v-table-background-color or valo-table-background-color();
  $border-color: $v-table-border-color or first-color(valo-border($color: $background-color, $strength: 0.8));

  .#{$primary-stylename}-drag-center {
    border: 2px solid $c-table-drag-indicator-color;
  }

  .#{$primary-stylename}-arow-wrap {
    @include box-defaults;
    @include valo-gradient($cuba-table-aggregation-background-color);

    border-top: first-number($v-border) solid $border-color;
    border-left: first-number($v-border) solid $border-color;
    border-right: first-number($v-border) solid $border-color;
  }

  .#{$primary-stylename}-arow {
    @include box-defaults;
  }

  .#{$primary-stylename}-arow-row {
    @include box-defaults;

    height: $v-table-row-height;

    border-right: first-number($v-border) solid $border-color;
    color: valo-font-color($cuba-table-aggregation-background-color, 0.9);
  }

  .#{$primary-stylename}-arow-row > .#{$primary-stylename}-cell-content {
    border-bottom: 0;
  }

  .#{$primary-stylename}-footer-wrap {
    @include valo-gradient($cuba-table-aggregation-background-color);
  }

  .#{$primary-stylename},
  .#{$primary-stylename}-body {
    font-size: $v-font-size;
  }

  .#{$primary-stylename}-cell-content.boolean-cell-true .#{$primary-stylename}-cell-wrapper,
  .#{$primary-stylename}-cell-content.boolean-cell-false .#{$primary-stylename}-cell-wrapper {
    color: transparent;
    height: round($v-unit-size/2);
    line-height: round($v-unit-size/2);
    width: round($v-unit-size/2);
    font-size: 0;
  }

  .#{$primary-stylename}-cell-content.boolean-cell-true .#{$primary-stylename}-cell-wrapper {
    &:before {
      color: $v-font-color;
      font-size: $v-font-size;
      font-family: FontAwesome;
      content: "\f046";
    }
  }

  .#{$primary-stylename}-cell-content.boolean-cell-false .#{$primary-stylename}-cell-wrapper {
    &:before {
      color: $v-font-color;
      font-size: $v-font-size;
      font-family: FontAwesome;
      content: "\f096";
    }
  }

  .v-selected {
    .#{$primary-stylename}-cell-content.boolean-cell-true .#{$primary-stylename}-cell-wrapper,
    .#{$primary-stylename}-cell-content.boolean-cell-false .#{$primary-stylename}-cell-wrapper {
      &:before {
        $font-color: valo-font-color($v-selection-color, 0.9);
        color: $font-color;
      }
    }
  }

  .#{$primary-stylename}-column-selector {
    @include valo-button-style;

    z-index: 2;
    top: round($v-unit-size/2) - round($v-unit-size/4);
    right: round($v-unit-size/2) - round($v-unit-size/4);
    height: round($v-unit-size/2);
    line-height: round($v-unit-size/2);
    width: round($v-unit-size/2);
    padding: 0;
    border-radius: 50%;

    @include opacity(0);
    @if $v-animations-enabled {
      @include transition(opacity 200ms 1s);
    }

    &:after {
      border-radius: inherit;
    }

    &:active:after {
      @include valo-button-active-style;
    }
  }

  .v-scrollable > .#{$primary-stylename} .#{$primary-stylename}-column-selector {
    top: round($v-unit-size/2) - round($v-unit-size/4);
    right: round($v-unit-size/2) - round($v-unit-size/4);
  }

  .#{$primary-stylename} > .#{$primary-stylename}-header-wrap {
    font-weight: $cuba-table-header-font-weight;
  }

  .#{$primary-stylename}.v-has-width {
    & > .#{$primary-stylename}-body-wrapper,
    & > .#{$primary-stylename}-header-wrap,
    & > .#{$primary-stylename}-footer-wrap {
      width: 100% !important;
      @include box-defaults;
    }
  }

  .v-scrollable > .#{$primary-stylename} {
    overflow: auto;
  }

  .#{$primary-stylename}-cell-wrapper {
    > .v-widget {
      margin-left: 0;
      margin-right: 0;
    }
  }

  .c-table-cell-link .c-table-clickable-cell {
    @include valo-link-style;
  }

  .#{$primary-stylename} {
    .c-table-cell-link .c-table-clickable-cell,
    .v-button-link,
    .v-link {
      -webkit-transition: none;
      -moz-transition: none;
      transition: none;
    }
  }

  .#{$primary-stylename}.v-disabled {
    @include opacity($v-disabled-opacity);
  }

  .c-table-cell-textcut .c-table-clickable-cell {
    cursor: $v-link-cursor;
    color: $v-link-font-color;
    border-bottom: 1px dashed $v-link-font-color;
  }

  .#{$primary-stylename} [class*="-row"].v-selected {
    $font-color: valo-font-color($v-selection-color, 0.9);

    .c-table-cell-link .c-table-clickable-cell,
    .c-table-cell-textcut .c-table-clickable-cell,
    .v-popupview,
    .v-button-link,
    .v-link {
      color: $font-color;
    }

    .c-table-cell-textcut .c-table-clickable-cell {
      border-bottom-color: $font-color;
    }
  }

  .c-table-view-textcut {
    @include valo-selection-overlay-style;
  }

  .v-ios.v-touch & .#{$primary-stylename}-column-selector,
  .v-android.v-touch & .#{$primary-stylename}-column-selector {
    @include opacity(1);
    @include transition(none);
  }

  .#{$primary-stylename}-header-cell-asc .#{$primary-stylename}-sort-indicator,
  .#{$primary-stylename}-header-cell-desc .#{$primary-stylename}-sort-indicator {
    cursor: pointer;
  }

  .#{$primary-stylename}-header-cell.#{$primary-stylename}-header-sortable .#{$primary-stylename}-sort-indicator {
    display: block;
    background: transparent;
    width: round($v-unit-size/2);
    height: $v-table-row-height;
    line-height: $v-table-row-height;
    margin-left: -$v-table-sort-indicator-width;
    opacity: 0;
    cursor: pointer;
  }

  .c-table-contextmenu {
    @include valo-selection-overlay-style;
  }

  .c-table-contextmenu .c-table-contextmenu-item {
    @include valo-selection-item-style;
    display: block; // Firefox 24 needs this to make position: relative; work
  }

  .c-table-contextmenu .c-table-contextmenu-item:hover {
    @include valo-selection-item-selected-style;
  }

  .c-table-composition.has-top-panel {
    display: flex;
    flex-direction: column;

    .c-table-top {
      display: flex;
      justify-content: space-between;
      flex-shrink: 0;
      overflow-x: hidden;
      min-height: $v-unit-size;
    }

    // The caption of ButtonsPanel inside of Table will be ignored
    .v-caption-c-buttons-panel {
      display: none;
    }

    .v-ie11 &,
    .v-edge & {
      .c-table-top {
        overflow-y: hidden;
      }

      .c-buttons-panel {
        overflow-x: hidden
      }
    }
  }

  @if $cuba-buttonspanel-flow == false {
    .c-table-composition {
      .v-csslayout.c-flowlayout {
        white-space: nowrap;
      }

      .v-csslayout.c-flowlayout > * {
        float: none;
      }
    }

    .c-table-composition.has-top-panel {
      $top-panel-height: $v-unit-size + ceil($v-layout-spacing-vertical);

      display: block;
      padding-top: $top-panel-height;

      .c-table-top {
        margin-top: -1 * $top-panel-height;
      }
    }
  }

  .v-ff & .v-table-header tr > .c-grouptable-group-divider-header:first-child .v-table-caption-container {
    padding-left: 8px;
    border-left: 0;
  }

  .c-table-composition {
    font-size: 0;

    & > * {
      font-size: $v-font-size;
    }

    &.no-stripes {
      .v-table {
        @include valo-table-no-stripes-style;

        [class*="-row"].v-selected {
            $selected-border-color: adjust-color($v-selection-color, $lightness: -8%, $saturation: -8%);

            @include valo-gradient($v-selection-color);
            background-origin: border-box;
            $font-color: valo-font-color($v-selection-color, 0.9);
            color: $font-color;
            text-shadow: valo-text-shadow($font-color: $font-color, $background-color: $v-selection-color);

            + .v-selected {
              $gradient-end: first(last(valo-gradient-color-stops($v-selection-color)));
              background: $gradient-end;

              td {
                border-top-color: $gradient-end;
              }
            }

            .#{$primary-stylename}-cell-content {
              border-color: transparent;
              border-left-color: $selected-border-color;

              &:first-child {
                border-left-color: transparent;
              }
            }
        }
      }
    }

    &.no-vertical-lines {
      .v-table {
        @include valo-table-no-vertical-lines-style;
      }

      .v-table-caption-container,
      .v-table-footer td {
        border-left-color: transparent;
      }
    }

    &.no-horizontal-lines {
      .v-table {
        @include valo-table-no-horizontal-lines-style;
      }
    }

    &.no-header {
      .v-table {
        @include valo-table-no-header-style;
      }
    }

    &.borderless {
      .v-table {
        @include valo-table-borderless-style;
      }

      .v-table-header table,
      .v-table-footer table,
      .v-table-table {
        @include box-shadow(none);
      }
    }

    &.compact,
    &.small {
      .v-table {
        @include valo-table-spacing-style($row-height: $v-unit-size--small, $cell-padding-horizontal: round($v-table-cell-padding-horizontal / 2));
      }
    }

    &.small {
      .v-table {
        font-size: $v-font-size--small;

        &.v-treetable .#{$primary-stylename}-cell-wrapper {
          min-height: $v-font-size--small;
        }
      }
    }
  }

  .#{$primary-stylename}-column-selector-popup .gwt-MenuBar {
    overflow-y: auto;
    overflow-x: hidden;

    table {
      border-collapse: collapse;
    }

    tr:nth-child(2) {
      border-bottom: valo-border();
      border-bottom-style: dotted;
    }
  }

  @if $cuba-table-highlight-hover-row {
    .#{$primary-stylename}-row:hover,
    .#{$primary-stylename}-row-odd:hover {
      background-color: $cuba-hover-color;
      color: valo-font-color($cuba-hover-color, 0.9);

      .#{$primary-stylename}-cell-content.boolean-cell-true .#{$primary-stylename}-cell-wrapper,
      .#{$primary-stylename}-cell-content.boolean-cell-false .#{$primary-stylename}-cell-wrapper {
        &:before {
          color: valo-font-color($cuba-hover-color, 0.9);
        }
      }
    }

    .#{$primary-stylename}-row.v-selected:hover,
    .#{$primary-stylename}-row-odd.v-selected:hover {
      @include valo-gradient($v-selection-color);
      background-origin: border-box;
      color: valo-font-color($v-selection-color, 0.9);

      .#{$primary-stylename}-cell-content.boolean-cell-true .#{$primary-stylename}-cell-wrapper,
      .#{$primary-stylename}-cell-content.boolean-cell-false .#{$primary-stylename}-cell-wrapper {
        &:before {
          color: valo-font-color($v-selection-color, 0.9);
        }
      }
    }
  }
}