/**
 * Copyright (c) Enalean, 2016-Present. All Rights Reserved.
 *
 * This file is a part of Tuleap.
 *
 * Tuleap is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Tuleap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
 */

@use "sass:color";
@use "sass:math";

$tlp-button-box-shadow-timing: 75ms;
$tlp-button-background-timing: 75ms;
$tlp-button-normal-height: 40px;
$tlp-button-normal-line-height: 1.5rem;
$tlp-button-normal-font-size: 0.875rem;
$tlp-button-ellipsis-normal-line-height: 1.875rem;
$tlp-button-ellipsis-normal-font-size: 1.875rem;
$tlp-button-normal-font-weight: 500;
$tlp-button-normal-padding: 0 var(--tlp-medium-spacing);
$tlp-button-normal-radius: var(--tlp-medium-radius);
$tlp-button-large-height: 48px;
$tlp-button-large-line-height: 1.5rem;
$tlp-button-large-font-size: 1.25rem;
$tlp-button-ellipsis-large-line-height: 2.25rem;
$tlp-button-ellipsis-large-font-size: 2.25rem;
$tlp-button-large-font-weight: 500;
$tlp-button-large-padding: 0 var(--tlp-large-spacing);
$tlp-button-large-radius: var(--tlp-medium-radius);
$tlp-button-small-height: 32px;
$tlp-button-small-line-height: 1.5rem;
$tlp-button-small-font-size: 0.75rem;
$tlp-button-ellipsis-small-line-height: 1.5rem;
$tlp-button-ellipsis-small-font-size: 1.5rem;
$tlp-button-small-font-weight: 500;
$tlp-button-small-padding: 0 var(--tlp-medium-spacing);
$tlp-button-small-radius: var(--tlp-small-radius);
$tlp-button-mini-height: 24px;
$tlp-button-mini-line-height: 1.15rem;
$tlp-button-mini-font-size: 0.75rem;
$tlp-button-ellipsis-mini-line-height: 1.25rem;
$tlp-button-ellipsis-mini-font-size: 1.25rem;
$tlp-button-mini-font-weight: 500;
$tlp-button-mini-padding: 0 var(--tlp-small-spacing);
$tlp-button-mini-radius: var(--tlp-small-radius);
$tlp-button-outline-hover-color: var(--tlp-neutral-light-color-lighter-30);

%-tlp-button {
    display: inline-flex;
    gap: 8px;
    align-items: center;
    justify-content: center;
    height: $tlp-button-normal-height;
    padding: $tlp-button-normal-padding;
    overflow: hidden;
    transition:
        box-shadow $tlp-button-box-shadow-timing ease-in-out,
        background $tlp-button-background-timing ease-in-out,
        border-color $tlp-button-background-timing ease-in-out;
    border: 1px solid transparent;
    border-radius: $tlp-button-normal-radius;
    box-shadow: var(--tlp-button-shadow);
    color: var(--tlp-white-color);
    font-family: var(--tlp-font-family);
    font-size: $tlp-button-normal-font-size;
    font-weight: $tlp-button-normal-font-weight;
    letter-spacing: 0;
    line-height: $tlp-button-normal-line-height;
    text-decoration: none;
    text-overflow: ellipsis;
    text-transform: none;
    vertical-align: middle;
    white-space: nowrap;
    cursor: pointer;

    &:hover,
    &:active,
    &:focus {
        transition:
            box-shadow $tlp-button-box-shadow-timing ease-in-out,
            background $tlp-button-background-timing ease-in-out,
            border-color $tlp-button-background-timing ease-in-out;
        box-shadow: var(--tlp-button-shadow-hover);
        text-decoration: none;
    }

    &:focus {
        box-shadow: var(--tlp-shadow-focus);
    }

    &:disabled,
    &.disabled {
        box-shadow: none;
        cursor: not-allowed;
    }

    &.tlp-button-round {
        width: $tlp-button-normal-height;
        padding: 0;
        border-radius: math.div($tlp-button-normal-height, 2);
    }

    > .tlp-button-icon {
        font-size: 0.75rem;
    }
}

@mixin -tlp-button-disabled($background-color, $text-color) {
    border-color: transparent;
    background: $background-color;
    color: $text-color;
}

@mixin -tlp-button-outline(
    $outline-color,
    $outline-color-disabled,
    $outline-background-color-hover,
    $outline-background-color-active
) {
    border: 1px solid var(--tlp-white-color);
    background: var(--tlp-white-color);
    color: $outline-color;

    &:hover,
    &:focus {
        background: $outline-background-color-hover;
    }

    &:active {
        background: $outline-background-color-active;
    }

    &:disabled,
    &.disabled {
        color: $outline-color-disabled;

        &:hover {
            background: var(--tlp-white-color);
        }
    }
}

@mixin -tlp-button-type(
    $button-color,
    $button-color-focus,
    $button-color-active,
    $button-color-disabled,
    $disabled-button-text-color,
    $outline-text-color,
    $outline-background-color-hover,
    $outline-background-color-active,
    $outline-color-disabled
) {
    border-color: $button-color;
    background: $button-color;

    &:hover,
    &:focus {
        border-color: $button-color-focus;
        background: $button-color-focus;
    }

    &:active {
        border-color: $button-color-active;
        background: $button-color-active;
    }

    &:disabled,
    &.disabled {
        @include -tlp-button-disabled($button-color-disabled, $disabled-button-text-color);
    }

    &.tlp-button-outline {
        @include -tlp-button-outline(
            $outline-text-color,
            $outline-color-disabled,
            $outline-background-color-hover,
            $outline-background-color-active
        );
    }

    &.tlp-button-ellipsis {
        --tlp-button-ellipsis-color: #{$outline-text-color};
        --tlp-button-ellipsis-color-on-dark-value: var(--tlp-white-color);
        --tlp-button-ellipsis-text-shadow: none;
        --tlp-button-ellipsis-text-shadow-on-dark-value: 0 1px 2px rgb(0 0 0 / 30%);
        --tlp-button-ellipsis-hover-color: #{$outline-background-color-hover};
        --tlp-button-ellipsis-hover-color-on-dark-value: rgb(0 0 0 / 30%);
        --tlp-button-ellipsis-focus-border-color: var(--tlp-white-color);
        --tlp-button-ellipsis-focus-border-color-on-dark-value: transparent;
        --tlp-button-ellipsis-disabled-color: #{$outline-color-disabled};
        --tlp-button-ellipsis-disabled-color-on-dark-value: var(--tlp-white-color);
        --tlp-button-ellipsis-disabled-opacity: 1;
        --tlp-button-ellipsis-disabled-opacity-on-dark-value: 0.5;

        border: 1px solid transparent;
        background: none;
        box-shadow: none;
        color: var(--tlp-button-ellipsis-color);
        font-size: $tlp-button-ellipsis-normal-font-size;
        line-height: $tlp-button-ellipsis-normal-line-height;
        text-shadow: var(--tlp-button-ellipsis-text-shadow);

        &:hover {
            background: var(--tlp-button-ellipsis-hover-color);
        }

        &:focus {
            border: 1px solid var(--tlp-button-ellipsis-focus-border-color);
            background: var(--tlp-button-ellipsis-hover-color);
            box-shadow: var(--tlp-shadow-focus);
        }

        &:active {
            background: var(--tlp-button-ellipsis-hover-color);
        }

        &:disabled,
        &.disabled {
            opacity: var(--tlp-button-ellipsis-disabled-opacity);
            color: var(--tlp-button-ellipsis-disabled-color);

            &:hover {
                background: none;
            }
        }
    }
}

/**
 * Types
 */
.tlp-button-primary {
    @extend %-tlp-button;
    @include -tlp-button-type(
        var(--tlp-main-color),
        var(--tlp-main-color-darker-05),
        var(--tlp-main-color-darker-10),
        var(--tlp-main-color-lighter-30),
        var(--tlp-white-color-transparent-40),
        var(--tlp-main-color),
        var(--tlp-main-color-lighter-90),
        var(--tlp-main-color-lighter-80),
        var(--tlp-main-color-transparent-50)
    );
}

.tlp-button-secondary {
    @extend %-tlp-button;

    border-color: var(--tlp-neutral-light-color);
    background: var(--tlp-neutral-light-color);
    color: var(--tlp-dark-color);

    &:hover,
    &:focus {
        border-color: var(--tlp-neutral-light-color-darker-05);
        background: var(--tlp-neutral-light-color-darker-05);
    }

    &:active {
        border-color: var(--tlp-neutral-light-color-darker-10);
        background: var(--tlp-neutral-light-color-darker-10);
    }

    &:disabled,
    &.disabled {
        @include -tlp-button-disabled(
            var(--tlp-neutral-light-color-lighter-30),
            var(--tlp-dark-color-transparent-50)
        );
    }

    &.tlp-button-outline {
        @include -tlp-button-outline(
            var(--tlp-dark-color),
            var(--tlp-neutral-light-color-lighter-30),
            var(--tlp-neutral-light-color-lighter-40),
            var(--tlp-dark-color-lighter-90)
        );
    }
}

.tlp-button-info {
    @extend %-tlp-button;
    @include -tlp-button-type(
        var(--tlp-info-color),
        var(--tlp-info-color-darker-05),
        var(--tlp-info-color-darker-10),
        var(--tlp-info-color-lighter-30),
        var(--tlp-white-color-transparent-40),
        var(--tlp-info-dark-color),
        var(--tlp-info-color-lighter-90),
        var(--tlp-info-color-lighter-80),
        var(--tlp-info-color-transparent-50)
    );
}

.tlp-button-success {
    @extend %-tlp-button;
    @include -tlp-button-type(
        var(--tlp-success-color),
        var(--tlp-success-color-darker-05),
        var(--tlp-success-color-darker-10),
        var(--tlp-success-color-lighter-30),
        var(--tlp-white-color-transparent-40),
        var(--tlp-success-dark-color),
        var(--tlp-success-color-lighter-90),
        var(--tlp-success-color-lighter-80),
        var(--tlp-success-color-transparent-50)
    );
}

.tlp-button-warning {
    @extend %-tlp-button;
    @include -tlp-button-type(
        var(--tlp-warning-color),
        var(--tlp-warning-color-darker-05),
        var(--tlp-warning-color-darker-10),
        var(--tlp-warning-color-lighter-30),
        var(--tlp-white-color-transparent-40),
        var(--tlp-warning-dark-color),
        var(--tlp-warning-color-lighter-90),
        var(--tlp-warning-color-lighter-80),
        var(--tlp-warning-color-transparent-50)
    );
}

.tlp-button-danger {
    @extend %-tlp-button;
    @include -tlp-button-type(
        var(--tlp-danger-color),
        var(--tlp-danger-color-darker-05),
        var(--tlp-danger-color-darker-10),
        var(--tlp-danger-color-lighter-30),
        var(--tlp-white-color-transparent-40),
        var(--tlp-danger-dark-color),
        var(--tlp-danger-color-lighter-90),
        var(--tlp-danger-color-lighter-80),
        var(--tlp-danger-color-transparent-50)
    );
}

/**
 * Sizes
 */
.tlp-button-large {
    height: $tlp-button-large-height;
    padding: $tlp-button-large-padding;
    border-radius: $tlp-button-large-radius;
    font-size: $tlp-button-large-font-size;
    font-weight: $tlp-button-large-font-weight;
    line-height: $tlp-button-large-line-height;

    &.tlp-button-ellipsis {
        font-size: $tlp-button-ellipsis-large-font-size;
        line-height: $tlp-button-ellipsis-large-line-height;
    }

    > .tlp-button-icon {
        font-size: 0.875rem;
    }
}

.tlp-button-small {
    gap: 4px;
    height: $tlp-button-small-height;
    padding: $tlp-button-small-padding;
    border-radius: $tlp-button-small-radius;
    font-size: $tlp-button-small-font-size;
    font-weight: $tlp-button-small-font-weight;
    line-height: $tlp-button-small-line-height;

    &.tlp-button-ellipsis {
        font-size: $tlp-button-ellipsis-small-font-size;
        line-height: $tlp-button-ellipsis-small-line-height;
    }

    > .tlp-button-icon {
        font-size: 0.625rem;
    }
}

.tlp-button-mini {
    gap: 4px;
    height: $tlp-button-mini-height;
    padding: $tlp-button-mini-padding;
    border-radius: $tlp-button-mini-radius;
    box-shadow:
        0 1px 2px rgb(0 0 0 / 8%),
        0 1px 1px rgb(0 0 0 / 10%),
        0 -1px 0 rgb(0 0 0 / 3%);
    font-size: $tlp-button-mini-font-size;
    font-weight: $tlp-button-mini-font-weight;
    line-height: $tlp-button-mini-line-height;

    &.tlp-button-ellipsis {
        font-size: $tlp-button-ellipsis-mini-font-size;
        line-height: $tlp-button-ellipsis-mini-line-height;
    }

    > .tlp-button-icon {
        font-size: 0.625rem;
    }
}

/**
 * Wide
 */
.tlp-button-wide {
    width: 100%;
    text-align: center;
}

/**
 * Overrides for a.tlp-button* so they have the good heights
 */
a[class*="tlp-button"] {
    height: auto;
    line-height: $tlp-button-normal-height - 2px;
    text-align: center;
}

// stylelint-disable-next-line selector-no-qualifying-type
a.tlp-button-large[class*="tlp-button"] {
    line-height: $tlp-button-large-height - 2px;
}

// stylelint-disable-next-line selector-no-qualifying-type
a.tlp-button-small[class*="tlp-button"] {
    line-height: $tlp-button-small-height - 2px;
}

// stylelint-disable-next-line selector-no-qualifying-type
a.tlp-button-mini[class*="tlp-button"] {
    line-height: $tlp-button-mini-height - 2px;
}
