/*
* 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.
*/
@use '@angular/material' as mat;
@use "sass:map";

@import "./app/core/servers/server-details/server-details-theme";
@import "./app/shared/tree-select/tree-select-theme";
@import "./app/shared/navigation/tp-sidebar/tp-sidebar-theme";
@import "../node_modules/ag-grid-community/src/styles/ag-grid.scss";
@import "../node_modules/ag-grid-community/src/styles/ag-theme-material/sass/ag-theme-material-mixin";

@mixin theme-color($config-or-theme) {
	// Extract the color configuration in case a theme has been passed.
	// This allows consumers to either pass a theme object or a color configuration.
	$config: mat.get-color-config($config-or-theme);
	// Extract the palettes you need from the theme definition.
	$primary: map-get($config, primary);
	$accent: map-get($config, accent);
	$warn: map-get($config, warn);
	$background: map-get($config, background);
	$foreground: map-get($config, foreground);

	body {
		background-color: mat.get-color-from-palette($background, background);
	}

	.color-primary {
		// Use mat-color to extract individual colors from a palette.
		background-color: mat.get-color-from-palette($background, background);
		color: white;
		border-color: mat.get-color-from-palette($config, A400);
	}

	.color-primary-inverted {
		background-color: transparent;
		color: mat.get-color-from-palette($primary);
	}

	.color-accent {
		background-color: mat.get-color-from-palette($accent);
		color: white;
		border-color: mat.get-color-from-palette($accent, A400);
	}

	.color-accent-inverted {
		background-color: transparent;
		color: mat.get-color-from-palette($accent);
	}

	.color-warn {
		background-color: mat.get-color-from-palette($warn);
		color: white;
		border-color: mat.get-color-from-palette($warn, A400);
	}

	.color-warn-inverted {
		background-color: white;
		color: mat.get-color-from-palette($warn);
	}

	span.warning {
		background-color: mat.get-color-from-palette($warn);
		color: white;
		&::before {
			content: " ";
			font-family: "FontAwesome";
			color: inherit;
			padding: 2px 5px;
		}
	}

}

@mixin theme-typography($config-or-theme) {
	// Extract the typography configuration in case a theme has been passed.
	$config: mat.get-typography-config($config-or-theme);

	.color-primary,
	.color-primary-inverted,
	.color-accent,
	.color-accent-inverted,
	.color-warn,
	.color-warn-inverted {
		font: {
			family: mat.font-family($config, body-1);
			size: mat.font-size($config, body-1);
			weight: mat.font-weight($config, body-1);
		}
	}
}

@mixin theme($theme) {
	// Extracts the color and typography configurations from the theme.
	$color: mat.get-color-config($theme);
	$typography: mat.get-typography-config($theme);

	// Do not generate styles if configurations for individual theming
	// systems have been explicitly set to `null`.
	@if $color != null {
		@include theme-color($color);
		@include grid-theme($theme);
		@include server-details-theme($color);
		@include tree-select-theme($color);
		@include tp-sidebar-theme($color);
	}
	@if $typography != null {
		@include theme-typography($typography);
	}
}

@mixin grid-theme($theme) {

	$color: mat.get-color-config($theme);
	$primary: map-get($color, primary);
	$accent: map-get($color, accent);
	$background: map-get($color, background);
	$foreground: map-get($color, foreground);
	$ag-grid: map-get($theme, ag-grid);

	.ag-theme-material {
		@include ag-theme-material((
			material-primary-color:  mat.get-color-from-palette($primary),
			material-accent-color: mat.get-color-from-palette($accent),
			foreground-color: mat.get-color-from-palette($foreground, foreground),
			background-color: mat.get-color-from-palette($background, background),
			secondary-foreground-color: mat.get-color-from-palette($foreground, foreground),
			header-background-color: mat.get-color-from-palette($background, background),
			row-hover-color: map-get($background, hover),
			selected-row-background-color: map-get($background, hover),
			odd-row-background-color: map-get($ag-grid, odd-row-background-color),
			row-border-color: map-get($ag-grid, row-border-color),
			border-color: map-get($ag-grid, border-color)
		));
	}
}

@function set-background($theme, $bgcolor) {
	$color: map.get($theme, color);
	$bg: map.get($color, background);
	$bg: map.merge($bg, $bgcolor);
	$color: map.merge($color, (background: $bg));
	@return map.merge($theme, (color: $color));
}
@function set-foreground($theme, $fgcolor) {
	$color: map.get($theme, color);
	$bg: map.get($color, background);
	$bg: map.merge($bg, $fgcolor);
	$color: map.merge($color, (background: $bg));
	@return map.merge($theme, (color: $color));
}
@function set-ag-grid($theme, $ag-grid) {
	@return map.merge($theme, (ag-grid: $ag-grid));
}
@function add-extra-colors($theme, $colors) {
	$color: map.get($theme, color);
	$color: map.merge($color, $colors);
	@return map.merge($theme, (color: $color));
}
