import { ChangeDetectionStrategy, ChangeDetectorRef, Component, computed, DoCheck, inject, Injector, input, OnInit, output, signal } from '@angular/core';
import { FAST_KENDO_COMMON } from '../../app.config';
import { ControlValueAccessor, FormControl, FormControlDirective, FormControlName, FormGroupDirective, NG_VALUE_ACCESSOR, NgControl } from '@angular/forms';
import { DropDownFilterSettings } from '@progress/kendo-angular-dropdowns';
import { isMobileDevice } from '../utils/util';

@Component({
  selector: 'fast-dropdown',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [FAST_KENDO_COMMON],
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    multi: true,
    useExisting: FastDropdownComponent
  },
  {
    provide: NgControl,
    multi: true,
    useExisting: FastDropdownComponent
  }],
  template: `
  <kendo-dropdownlist
    [data]="data()"
    [textField]="textField()"
    [valueField]="valueField()"
    [valuePrimitive]="valuePrimitive()"
    [kendoDropDownFilter]="filterSettings"
    [value]="internalValue()"
    [virtual]="{ itemHeight: isMobileDevice() ? 48 : 28 }"
    (valueChange)="onValueChange($event)"
    (filterChange)="onFilterChange($event)"
    (selectionChange)="onSelectionChange($event)"
    [class]="conditionalClasses()"
    [disabled]="isDisabled()"
    [readonly]="readOnly()"
    [adaptiveMode]="'auto'"
    [filterable]="false"
  />
  `
})
export class FastDropdownComponent<T extends object | string> implements ControlValueAccessor, OnInit, DoCheck {
  cdr = inject(ChangeDetectorRef);
  injector = inject(Injector);
  isMobileDevice = isMobileDevice;

  data = input.required<T[]>();
  textField = input<string>(null);
  valueField = input<string>(null);
  valuePrimitive = input<boolean>(true);
  clearButton = input<boolean>(false);
  placeholder = input<string>('');
  disabled = input<boolean>(false);
  valueChange = output<T | null>();
  filterChange = output<string>();
  selectionChange = output<T | null>();
  value = input<T | null>(null);
  internalValue = signal<T | null>(null);
  clearOnValueChange = input<boolean>(false);
  readOnly = input<boolean>(false);
  invalidTrigger = signal(0);
  formControl: FormControl;

  // Internal signal to track form control disabled state
  private formControlDisabled = signal<boolean>(false);

  // Computed signal that combines input disabled state and form control disabled state
  isDisabled = computed(() => this.disabled() || this.formControlDisabled());

  onChange: (value: T | null) => void = () => { };
  onTouched = () => { };

  ngDoCheck() {
    //needed so that isInvalid is recomputed when the form control is touched with changing the value
    // and when `markAllAsTouched` is called on the parent form
    if (this.formControl?.touched)
      this.invalidTrigger.update((v) => v + 1);
  }

  ngOnInit() {
    const ngControl = this.injector.get(NgControl);

    if (ngControl instanceof FormControlName) {
      this.formControl = this.injector
        .get(FormGroupDirective)
        .getControl(ngControl)
    } else {
      this.formControl = (ngControl as FormControlDirective).form as FormControl;
    }
  }

  isInvalid = computed(() => {
    this.invalidTrigger();
    const control = this.formControl;
    if (control && !this.isDisabled())
      return control.invalid && (control.touched || control.dirty)
    else
      return false;
  });

  writeValue(value: T | null) {
    this.internalValue.set(value);
    this.cdr.markForCheck();
  }

  registerOnChange(fn: () => void) {
    this.onChange = fn;
  }

  registerOnTouched(fn: () => void) {
    this.onTouched = fn;
  }

  onValueChange(newValue: T | null) {
    this.internalValue.set(newValue);
    this.onChange(newValue);
    this.invalidTrigger.update((v) => v + 1);
    this.valueChange.emit(newValue);

    //If clearOnValueChange() is true then we need to set the value twice
    //Once with the new value so that the caller's valueChange event (if the event exists) is fired with the selected value
    //Then again with null so that the dropdown is cleared
    if (this.clearOnValueChange()) {
      setTimeout(() => {
        this.internalValue.set(null);
        this.onChange(null);
        this.invalidTrigger.update((v) => v + 1);
      });
    }
  }

  onFilterChange(event: string): void {
    this.filterChange.emit(event);
  }

  onSelectionChange(event: T | null): void {
    this.selectionChange.emit(event);
  }

  setDisabledState(isDisabled: boolean) {
    this.formControlDisabled.set(isDisabled);
    this.cdr.markForCheck();
  }

  conditionalClasses = computed(() => {
    const classes = [] as string[];

    classes.push(...this.getCommonClasses());
    classes.push(...this.getLightBaseClasses());
    classes.push(...this.getDarkBaseClasses());
    if (!this.isDisabled()) {
      classes.push(...this.getLightHoverClasses());
      classes.push(...this.getDarkHoverClasses());
      classes.push(...this.getLightActiveClasses());
      classes.push(...this.getDarkActiveClasses());
    }

    const conditionalClasses = this.getConditionalClassesFromArrays(classes);
    return conditionalClasses;
  });


  getConditionalClassesFromArrays(classArray: string[]): { [key: string]: boolean } {
    const classes: { [key: string]: boolean } = {};
    classArray.forEach(className => {
      classes[className] = true;
    });
    return classes;
  }

  getCommonClasses() {
    return [
      "h-7",
      "items-center",
      "[&_button]:border-none",
      "[&_button]:w-6.5",
      "[&_button]:items-center",
      "bg-[var(--kendo-color-app-surface)]"
    ];
  }

  getLightBaseClasses() {
    const classes = [
      "text-base-black-1000",
      "[&_button]:text-base-black-1000",
      "[&_button]:bg-base-white-250"];

    if (this.isInvalid()) {
      classes.push(
        "border-red-500",
        "ring-red-500/50",
        "ring-2"
      );
    } else {
      classes.push(
        "border-base-gray-500",
        "ring-base-blue-250/50"
      );
    }

    return classes;
  }

  getDarkBaseClasses() {
    const classes = [
      "dark:text-base-white-500",
      "dark:[&_button]:text-base-white-500",
      "dark:[&_button]:bg-alt-gray-500"];

    if (this.isInvalid()) {
      classes.push(
        "dark:border-red-500",
        "dark:ring-red-500/50"
      );
    } else {
      classes.push(
        "dark:border-alt-blue-250",
        "dark:ring-base-blurple-250/50"
      );
    }

    return classes;
  }

  getLightHoverClasses() {
    return [
      "[&_button]:hover:bg-base-white-750"];
  }

  getDarkHoverClasses() {
    return [
      "dark:[&_button]:hover:bg-alt-blue-750"];
  }

  getLightActiveClasses() {
    return [
      "[&_button]:active:bg-base-white-1000"];
  }

  getDarkActiveClasses() {
    return [
      "dark:[&_button]:active:bg-alt-blue-250"];
  }

  public filterSettings: DropDownFilterSettings = {
    caseSensitive: false,
    operator: "contains",
  };
}
