/*!
 * Copyright (C) 2023 StApps
 * This program 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, version 3.
 *
 * This program 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
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */
@use 'sass:color';
@use 'sass:math';
@import '../color-processing';
@import './dark';

@function to-rgb($color) {
  @return color.red($color) + ',' + color.green($color) + ',' + color.blue($color);
}

@function to-shade($color) {
  @return color.adjust($color, $lightness: -$shade-amount);
}

@function to-tint($color) {
  @return color.adjust($color, $lightness: $tint-amount);
}

@function to-contrast($color) {
  $yiq: to-yiq($color);
  @return if($yiq >= $contrast-threshold, $contrast-dark-color, $contrast-light-color);
}

@function fade($color, $amount) {
  @return if(
    color.lightness($color) > $fade-threshold,
    color.adjust($color, $lightness: -$amount),
    color.adjust($color, $lightness: $amount)
  );
}

@function map-range($value, $input-min, $input-max, $output-min, $output-max) {
  @return calc(($value - $input-min) * ($output-max - $output-min) / ($input-max - $input-min) + $output-min);
}

@function interpolate-colors($from, $to, $progress, $progress-min: 0, $progress-max: 100) {
  @return rgb(
    map-range($progress, $progress-min, $progress-max, color.red($from), color.red($to)),
    map-range($progress, $progress-min, $progress-max, color.green($from), color.green($to)),
    map-range($progress, $progress-min, $progress-max, color.blue($from), color.blue($to))
  );
}

/**
 * Converts a color to the YIQ color system
 */
@function to-yiq($color) {
  @return math.div(
    (color.red($color) * 299) + (color.green($color) * 587) + (color.blue($color) * 114),
    1000
  );
}

@mixin color-rgb($name, $color) {
  #{$name}: #{$color};
  #{$name}-rgb: #{to-rgb($color)};
}

@mixin light-dark-scheme($light, $dark) {
  :root,
  .light {
    @if $include-theme-independent-colors == true {
      @content ($light, -light);
      @content (if($dark == none, $light, $dark), -dark);
    }
    @content ($light, "");
  }
  @if $dark != none {
    @include dark-only {
      @content ($dark, "");
    }
  }
}

@mixin ion-color-step($name, $from, $to) {
  @for $i from calc($steps-start / $steps-step) through calc($steps-end / $steps-step) {
    $value: $i * $steps-step;

    #{$name}-#{$value}: #{interpolate-colors($from, $to, $value, $steps-range-start, $steps-range-end)};
  }
}

@mixin ion-color($name, $light, $dark: none) {
  @include light-dark-scheme($light, $dark) using($color, $suffix) {
    @include color-rgb(--ion-color-#{$name}#{$suffix}, $color);
    @include color-rgb(--ion-color-#{$name}-shade#{$suffix}, to-shade($color));
    @include color-rgb(--ion-color-#{$name}-tint#{$suffix}, to-tint($color));
    @include color-rgb(--ion-color-#{$name}-contrast#{$suffix}, to-contrast($color));
  }
}

@mixin ion-background-color($light, $dark: none) {
  @include light-dark-scheme($light, $dark) using($color, $suffix) {
    $text-color: to-contrast($color);
    @include color-rgb(--ion-background-color#{$suffix}, $color);
    @include color-rgb(--ion-text-color#{$suffix}, $text-color);
    @include color-rgb(--ion-box-shadow-color#{$suffix}, fade($color, $box-shadow-fade-amount));
    @include color-rgb(--ion-placeholder-color#{$suffix}, fade($text-color, $placeholder-fade-amount));

    @include ion-color-step(--ion-color-step#{$suffix}, $color, $text-color);
  }
}

@mixin ion-item-color($light, $dark: none) {
  @include light-dark-scheme($light, $dark) using($color, $suffix) {
    @include color-rgb(--ion-item-background#{$suffix}, $color);
    @include color-rgb(--ion-card-background#{$suffix}, $color);
    @include color-rgb(--ion-item-border-color#{$suffix}, fade($color, $item-border-color-fade-amount));
    @include color-rgb(--ion-border-color#{$suffix}, fade($color, $item-border-color-fade-amount));
    @include color-rgb(--ion-item-color#{$suffix}, to-contrast($color));
  }
}
