import { CommonModule } from '@angular/common'
import { Component, input } from '@angular/core'

@Component({
	selector: 'kadi-rating',
	template: `
		<div class="flex">
			@if (inRange(rating(), 0, 0.3)) {
				<ng-container [ngTemplateOutlet]="emptyIcon" />
			}
			@if (inRange(rating(), 0.3, 0.8)) {
				<ng-container [ngTemplateOutlet]="halfIcon" />
			}
			@if (inRange(rating(), 0.8, 6)) {
				<ng-container [ngTemplateOutlet]="fillIcon" />
			}

			@if (inRange(rating(), 0, 1.3)) {
				<ng-container [ngTemplateOutlet]="emptyIcon" />
			}
			@if (inRange(rating(), 1.3, 1.8)) {
				<ng-container [ngTemplateOutlet]="halfIcon" />
			}
			@if (inRange(rating(), 1.8, 6)) {
				<ng-container [ngTemplateOutlet]="fillIcon" />
			}

			@if (inRange(rating(), 0, 2.3)) {
				<ng-container [ngTemplateOutlet]="emptyIcon" />
			}
			@if (inRange(rating(), 2.3, 2.8)) {
				<ng-container [ngTemplateOutlet]="halfIcon" />
			}
			@if (inRange(rating(), 2.8, 6)) {
				<ng-container [ngTemplateOutlet]="fillIcon" />
			}

			@if (inRange(rating(), 0, 3.3)) {
				<ng-container [ngTemplateOutlet]="emptyIcon" />
			}
			@if (inRange(rating(), 3.3, 3.8)) {
				<ng-container [ngTemplateOutlet]="halfIcon" />
			}
			@if (inRange(rating(), 3.8, 6)) {
				<ng-container [ngTemplateOutlet]="fillIcon" />
			}

			@if (inRange(rating(), 0, 4.3)) {
				<ng-container [ngTemplateOutlet]="emptyIcon" />
			}
			@if (inRange(rating(), 4.3, 4.8)) {
				<ng-container [ngTemplateOutlet]="halfIcon" />
			}
			@if (inRange(rating(), 4.8, 6)) {
				<ng-container [ngTemplateOutlet]="fillIcon" />
			}
		</div>

		<ng-template #emptyIcon>
			<svg
				xmlns="http://www.w3.org/2000/svg"
				[style.width]="size() + 'px'"
				[style.height]="size() + 'px'"
				viewBox="0 0 24 24"
				[attr.fill]="color()">
				<path
					d="M0 0h24v24H0V0z"
					fill="none" />
				<path
					d="M22 9.24l-7.19-.62L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21 12 17.27 18.18 21l-1.63-7.03L22 9.24zM12 15.4l-3.76 2.27 1-4.28-3.32-2.88 4.38-.38L12 6.1l1.71 4.04 4.38.38-3.32 2.88 1 4.28L12 15.4z" />
			</svg>
		</ng-template>
		<ng-template #halfIcon>
			<svg
				xmlns="http://www.w3.org/2000/svg"
				[style.width]="size() + 'px'"
				[style.height]="size() + 'px'"
				viewBox="0 0 24 24"
				[attr.fill]="color()">
				<path
					d="M0 0h24v24H0z"
					fill="none" />
				<path
					d="M22 9.24l-7.19-.62L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21 12 17.27 18.18 21l-1.63-7.03L22 9.24zM12 15.4V6.1l1.71 4.04 4.38.38-3.32 2.88 1 4.28L12 15.4z" />
			</svg>
		</ng-template>
		<ng-template #fillIcon>
			<svg
				xmlns="http://www.w3.org/2000/svg"
				enable-background="new 0 0 24 24"
				[style.width]="size() + 'px'"
				[style.height]="size() + 'px'"
				viewBox="0 0 24 24"
				[attr.fill]="color()">
				<g>
					<path
						d="M0 0h24v24H0V0z"
						fill="none" />
					<path
						d="M0 0h24v24H0V0z"
						fill="none" />
				</g>
				<g>
					<path d="M12 17.27 18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21 12 17.27z" />
				</g>
			</svg>
		</ng-template>
	`,
	imports: [CommonModule],
})
export class RatingComponent {
	readonly rating = input.required<number>()
	readonly color = input('#FF9529')
	readonly size = input(24)

	inRange(x: number, min: number, max: number): boolean {
		return x >= min && x < max
	}
}
