@import '../../style/mixins/index.less';

// mixins for button
// ------------------------
.button-size(@height; @padding-horizontal; @font-size; @border-radius) {
  @padding-vertical: max((round(((@height - @font-size * @line-height-base) / 2) * 10) / 10) - @border-width-base,
    0);
  height: @height;
  padding: @padding-vertical @padding-horizontal;
  font-size: @font-size;
  border-radius: @border-radius;
}

.button-type-group() {
  .@{button-prefix-cls}-group &:not(:first-child):not(:last-child) {
    border-right-color: @button-group-border;
    border-left-color: @button-group-border;

    &:disabled {
      border-color: @button-default-border;
    }
  }

  .@{button-prefix-cls}-group &:first-child {
    &:not(:last-child) {
      border-right-color: @button-group-border;

      &[disabled] {
        border-right-color: @button-default-border;
      }
    }
  }

  .@{button-prefix-cls}-group &:last-child:not(:first-child),
  .@{button-prefix-cls}-group &+& {
    border-left-color: @button-group-border;

    &[disabled] {
      border-left-color: @button-default-border;
    }
  }
}

.button-disabled(@color: @button-disable-color; @background: @button-disable-bg; @border: @button-disable-border) {
  &[disabled] {

    &,
    &:hover,
    &:focus,
    &:active {
      .button-color(@color; @background; @border);

      text-shadow: none;
      box-shadow: none;
    }
  }
}

.button-variant-primary(@color; @background) {
  .button-color(@color; @background; @background);

  text-shadow: @button-text-shadow;
  box-shadow: @button-primary-shadow;

  &:hover,
  &:focus {
    & when (@theme =dark) {
      .button-color(@color; ~`colorPalette('@{background}', 7) `; ~`colorPalette('@{background}', 7) `);
    }

    & when not (@theme =dark) {
      .button-color(@color; ~`colorPalette('@{background}', 5) `; ~`colorPalette('@{background}', 5) `);
    }
  }

  &:active {
    & when (@theme =dark) {
      .button-color(@color; ~`colorPalette('@{background}', 5) `; ~`colorPalette('@{background}', 5) `);
    }

    & when not (@theme =dark) {
      .button-color(@color; ~`colorPalette('@{background}', 7) `; ~`colorPalette('@{background}', 7) `);
    }
  }

  .button-disabled();
}

.button-variant-other(@color; @background; @border) {
  .button-color(@color; @background; @border);

  &:hover,
  &:focus {
    & when (@theme =dark) {
      .button-color(@primary-5; @background; @primary-5);
    }

    & when not (@theme =dark) {
      .button-color(~`colorPalette('@{button-primary-bg}', 5) `; @background;
        ~`colorPalette('@{button-primary-bg}', 5) `);
    }
  }

  &:active {
    & when (@theme =dark) {
      .button-color(@primary-7; @background; @primary-7);
    }

    & when not (@theme =dark) {
      .button-color(~`colorPalette('@{button-primary-bg}', 7) `; @background;
        ~`colorPalette('@{button-primary-bg}', 7) `);
    }
  }

  .button-disabled();
}

.button-variant-ghost(@color; @border: @color) {
  .button-color(@color; transparent; @border);
  text-shadow: none;

  &:hover,
  &:focus {
    & when (@border =transparent) {
      & when (@theme =dark) {
        .button-color(~`colorPalette('@{color}', 7) `; transparent; transparent);
      }

      & when not (@theme =dark) {
        .button-color(~`colorPalette('@{color}', 5) `; transparent; transparent);
      }
    }

    & when not (@border =transparent) {
      & when (@theme =dark) {
        .button-color(~`colorPalette('@{color}', 7) `; transparent; ~`colorPalette('@{color}', 7) `);
      }

      & when not (@theme =dark) {
        .button-color(~`colorPalette('@{color}', 5) `; transparent; ~`colorPalette('@{color}', 5) `);
      }
    }
  }

  &:active {
    & when (@border =transparent) {
      & when (@theme =dark) {
        .button-color(~`colorPalette('@{color}', 5) `; transparent; transparent);
      }

      & when not (@theme =dark) {
        .button-color(~`colorPalette('@{color}', 7) `; transparent; transparent);
      }
    }

    & when not(@border =transparent) {
      & when (@theme =dark) {
        .button-color(~`colorPalette('@{color}', 5) `; transparent; ~`colorPalette('@{color}', 5) `);
      }

      & when not (@theme =dark) {
        .button-color(~`colorPalette('@{color}', 7) `; transparent; ~`colorPalette('@{color}', 7) `);
      }
    }
  }

  .button-disabled();
}

.button-color(@color; @background; @border) {
  color: @color;
  background: @background;
  border-color: @border; // a inside Button which only work in Chrome

  // http://stackoverflow.com/a/17253457
  >a:only-child {
    color: currentColor;

    &::after {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      background: transparent;
      content: '';
    }
  }
}

.button-group-base(@btnClassName) {
  position: relative;
  display: inline-flex;

  >.@{btnClassName},
  >span>.@{btnClassName} {
    position: relative;

    &:hover,
    &:focus,
    &:active {
      z-index: 2;
    }

    &[disabled] {
      z-index: 0;
    }
  }

  .@{btnClassName}-icon-only {
    font-size: @font-size-base;
  }

  // size
  &-lg>.@{btnClassName},
  &-lg>span>.@{btnClassName} {
    .button-size(@button-height-lg; @button-padding-horizontal-lg; @button-font-size-lg; 0);
  }

  &-lg .@{btnClassName}.@{btnClassName}-icon-only {
    .square(@button-height-lg);
    padding-right: 0;
    padding-left: 0;
  }

  &-sm>.@{btnClassName},
  &-sm>span>.@{btnClassName} {
    .button-size(@button-height-sm; @button-padding-horizontal-sm; @font-size-base; 0);

    >.@{iconfont-css-prefix} {
      font-size: @font-size-base;
    }
  }

  &-sm .@{btnClassName}.@{btnClassName}-icon-only {
    .square(@button-height-sm);
    padding-right: 0;
    padding-left: 0;
  }
}

// Base styles of buttons
// --------------------------------------------------
.btn() {
  position: relative;
  display: inline-block;
  font-weight: @button-font-weight;
  white-space: nowrap;
  text-align: center;
  background-image: none;
  border: @button-border-width @button-border-style transparent;
  box-shadow: @button-shadow;
  cursor: pointer;
  transition: all 0.3s @ease-in-out;
  user-select: none;
  touch-action: manipulation;
  .button-size(@button-height-base; @button-padding-horizontal-base; @font-size-base; @button-border-radius-base );

  >.@{iconfont-css-prefix} {
    line-height: 1;
  }

  &,
  &:active,
  &:focus {
    outline: 0;
  }

  &:not([disabled]):hover {
    text-decoration: none;
  }

  &:not([disabled]):active {
    outline: 0;
    box-shadow: none;
  }

  &[disabled] {
    cursor: not-allowed;

    >* {
      pointer-events: none;
    }
  }

  &-lg {
    .button-size(@button-height-lg; @button-padding-horizontal-lg; @button-font-size-lg; @button-border-radius-base );
  }

  &-sm {
    .button-size(@button-height-sm; @button-padding-horizontal-sm; @button-font-size-sm; @button-border-radius-sm );
  }
}

// primary button style
.btn-primary() {
  .button-variant-primary(@button-primary-color; @button-primary-bg);
}

.btn-blue() {
  .button-variant-primary(@button-blue-color; @button-blue-bg);
}

.btn-gray() {
  .button-variant-primary(@button-gray-color; @button-gray-bg);
}

// default button style
.btn-default() {
  .button-variant-other(@button-default-color; @button-default-bg; @button-default-border);

  &:hover,
  &:focus,
  &:active {
    text-decoration: none;
    background: @button-default-bg;
  }
}

// ghost button style
.btn-ghost() {
  .button-variant-other(@button-ghost-color, @button-ghost-bg, @button-ghost-border);
}

// dashed button style
.btn-dashed() {
  .button-variant-other(@button-default-color, @button-default-bg, @button-default-border);
  border-style: dashed;
}

// danger button style
.btn-danger() {
  .button-variant-primary(@button-danger-color, @button-danger-bg);
}

// danger default button style
.btn-danger-default() {
  .button-color(@error-color, @button-default-bg, @error-color);

  &:hover,
  &:focus {
    & when (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 7) `; @button-default-bg; ~`colorPalette('@{error-color}', 7) `);
    }

    & when not (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 5) `; @button-default-bg; ~`colorPalette('@{error-color}', 5) `);
    }
  }

  &:active {
    & when (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 5) `; @button-default-bg; ~`colorPalette('@{error-color}', 5) `);
    }

    & when not (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 7) `; @button-default-bg; ~`colorPalette('@{error-color}', 7) `);
    }
  }

  .button-disabled();
}

// danger link button style
.btn-danger-link() {
  .button-variant-other(@error-color, transparent, transparent);
  box-shadow: none;

  &:hover,
  &:focus {
    & when (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 7) `; transparent; transparent);
    }

    & when not (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 5) `; transparent; transparent);
    }
  }

  &:active {
    & when (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 5) `; transparent; transparent);
    }

    & when not (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 7) `; transparent; transparent);
    }
  }

  .button-disabled(@disabled-color; transparent; transparent);
}

// link button style
.btn-link() {
  .button-variant-other(@link-color, transparent, transparent);
  box-shadow: none;

  &:hover {
    background: @button-link-hover-bg;
  }

  &:hover,
  &:focus,
  &:active {
    border-color: transparent;
  }

  .button-disabled(@disabled-color; transparent; transparent);
}

// text button style
.btn-text() {
  .button-variant-other(@text-color, transparent, transparent);
  box-shadow: none;

  &:hover,
  &:focus {
    color: @text-color;
    background: @button-text-hover-bg;
    border-color: transparent;
  }

  &:active {
    color: @text-color;
    background: fadein(@button-text-hover-bg, 1%);
    border-color: transparent;
  }

  .button-disabled(@disabled-color; transparent; transparent);
}

.btn-danger-text() {
  .button-variant-other(@error-color, transparent, transparent);
  box-shadow: none;

  &:hover,
  &:focus {
    & when (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 7) `; @button-text-hover-bg; transparent);
    }

    & when not (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 5) `; @button-text-hover-bg; transparent);
    }
  }

  &:active {
    & when (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 5) `; fadein(@button-text-hover-bg, 1%); transparent);
    }

    & when not (@theme =dark) {
      .button-color(~`colorPalette('@{error-color}', 7) `; fadein(@button-text-hover-bg, 1%); transparent);
    }
  }

  .button-disabled(@disabled-color; transparent; transparent);
}

// round button
.btn-round(@btnClassName: btn) {
  .button-size(@button-circle-size; (@button-circle-size / 2); @font-size-base; @button-circle-size);

  &.@{btnClassName}-lg {
    .button-size(@button-circle-size-lg; (@button-circle-size-lg / 2); @button-font-size-lg; @button-circle-size-lg );
  }

  &.@{btnClassName}-sm {
    .button-size(@button-circle-size-sm; (@button-circle-size-sm / 2); @font-size-base; @button-circle-size-sm );
  }
}

// square button: the content only contains icon
.btn-square(@btnClassName: btn) {
  .square(@button-square-size);
  .button-size(@button-square-size; 0; @button-square-only-icon-size; @button-border-radius-base);

  &>* {
    font-size: @button-square-only-icon-size;
  }

  &.@{btnClassName}-lg {
    .square(@button-square-size-lg);
    .button-size(@button-square-size-lg; 0; @button-square-only-icon-size-lg; @button-border-radius-base);

    &>* {
      font-size: @button-square-only-icon-size-lg;
    }
  }

  &.@{btnClassName}-sm {
    .square(@button-square-size-sm);
    .button-size(@button-square-size-sm; 0; @button-square-only-icon-size-sm; @button-border-radius-base);

    &>* {
      font-size: @button-square-only-icon-size-sm;
    }
  }
}

// circle button: the content only contains icon
.btn-circle(@btnClassName: btn) {
  min-width: @button-height-base;
  padding-right: 0;
  padding-left: 0;
  text-align: center;
  border-radius: 50%;

  &.@{btnClassName}-lg {
    min-width: @button-height-lg;
    border-radius: 50%;
  }

  &.@{btnClassName}-sm {
    min-width: @button-height-sm;
    border-radius: 50%;
  }
}

// Horizontal button groups style
// --------------------------------------------------
.btn-group(@btnClassName: btn) {
  .button-group-base(@btnClassName);

  .@{btnClassName}+.@{btnClassName},
  .@{btnClassName}+&,
  span+.@{btnClassName},
  .@{btnClassName}+span,
  >span+span,
  &+.@{btnClassName},
  &+& {
    margin-left: -1px;
  }

  .@{btnClassName}-primary+.@{btnClassName}:not(.@{btnClassName}-primary):not([disabled]) {
    border-left-color: transparent;
  }

  .@{btnClassName} {
    border-radius: 0;
  }

  >.@{btnClassName}:first-child,
  >span:first-child>.@{btnClassName} {
    margin-left: 0;
  }

  >.@{btnClassName}:only-child {
    border-radius: @button-border-radius-base;
  }

  >span:only-child>.@{btnClassName} {
    border-radius: @button-border-radius-base;
  }

  >.@{btnClassName}:first-child:not(:last-child),
  >span:first-child:not(:last-child)>.@{btnClassName} {
    border-top-left-radius: @button-border-radius-base;
    border-bottom-left-radius: @button-border-radius-base;
  }

  >.@{btnClassName}:last-child:not(:first-child),
  >span:last-child:not(:first-child)>.@{btnClassName} {
    border-top-right-radius: @button-border-radius-base;
    border-bottom-right-radius: @button-border-radius-base;
  }

  &-sm {
    >.@{btnClassName}:only-child {
      border-radius: @button-border-radius-sm;
    }

    >span:only-child>.@{btnClassName} {
      border-radius: @button-border-radius-sm;
    }

    >.@{btnClassName}:first-child:not(:last-child),
    >span:first-child:not(:last-child)>.@{btnClassName} {
      border-top-left-radius: @button-border-radius-sm;
      border-bottom-left-radius: @button-border-radius-sm;
    }

    >.@{btnClassName}:last-child:not(:first-child),
    >span:last-child:not(:first-child)>.@{btnClassName} {
      border-top-right-radius: @button-border-radius-sm;
      border-bottom-right-radius: @button-border-radius-sm;
    }
  }

  &>& {
    float: left;
  }

  &>&:not(:first-child):not(:last-child)>.@{btnClassName} {
    border-radius: 0;
  }

  &>&:first-child:not(:last-child) {
    >.@{btnClassName}:last-child {
      padding-right: 8px;
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
    }
  }

  &>&:last-child:not(:first-child)>.@{btnClassName}:first-child {
    padding-left: 8px;
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
  }
}
