import { Component, input, signal } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideEye, lucideEyeOff } from '@ng-icons/lucide';
import { NgpButton } from 'ng-primitives/button';
import {
  injectPasswordState,
  NgpPassword,
  NgpPasswordInput,
  NgpPasswordToggle,
} from 'ng-primitives/password';
import { ChangeFn, provideValueAccessor, TouchedFn } from 'ng-primitives/utils';

@Component({
  selector: '<% if (prefix) { %><%= prefix %>-<% } %>password',
  hostDirectives: [NgpPassword],
  imports: [NgIcon, NgpPasswordInput, NgpPasswordToggle, NgpButton],
  providers: [provideValueAccessor(Password<%= componentSuffix %>), provideIcons({ lucideEye, lucideEyeOff })],
  template: `
    <input
      [value]="value()"
      [placeholder]="placeholder()"
      (input)="onValueChange($event)"
      ngpPasswordInput
      type="password"
      autocomplete="current-password"
    />
    <button ngpButton ngpPasswordToggle>
      <ng-icon [name]="state().visible() ? 'lucideEyeOff' : 'lucideEye'" />
    </button>
  `,
  styles: `
/* These styles rely on CSS variables that can be imported from ng-primitives/example-theme/index.css in your global styles */

    :host {
      display: block;
      position: relative;
    }

    [ngpPasswordInput] {
      height: 2.125rem;
      width: 100%;
      border-radius: 0.5rem;
      padding: 0 40px 0 12px;
      border: none;
      background: var(--ngp-background);
      color: var(--ngp-text-primary);
      font-size: 0.875rem;
      letter-spacing: -0.006em;
      box-shadow: var(--ngp-input-shadow);
      outline: none;
    }

    [ngpPasswordInput][data-focus] {
      outline: 2px solid var(--ngp-focus-ring);
      outline-offset: 2px;
    }

    [ngpPasswordInput]::placeholder {
      color: var(--ngp-text-placeholder);
    }

    [ngpPasswordToggle] {
      position: absolute;
      top: 0;
      right: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 2.125rem;
      width: 2.125rem;
      border: none;
      border-radius: 0.5rem;
      background-color: transparent;
      color: var(--ngp-text-tertiary);
      font-size: 1.125rem;
      cursor: pointer;
      outline: none;
      transition: color 150ms cubic-bezier(0.4, 0, 0.2, 1);
    }

    [ngpPasswordToggle][data-hover] {
      color: var(--ngp-text-secondary);
    }

    [ngpPasswordToggle][data-visible] {
      color: var(--ngp-primary);
    }

    [ngpPasswordToggle][data-focus-visible] {
      outline: 2px solid var(--ngp-focus-ring);
      outline-offset: -2px;
    }

    /* Reduce motion for users who prefer reduced motion. */
    @media (prefers-reduced-motion: reduce) {
      [ngpPasswordToggle] {
        transition-duration: 0s;
      }
    }
  `,
  host: {
    '(focusout)': 'onFocusOut($event)',
  },
})
export class Password<%= componentSuffix %> implements ControlValueAccessor {
  /** Access the password state to read the visibility. */
  protected readonly state = injectPasswordState();

  /** The placeholder text */
  readonly placeholder = input<string>('');

  /** The current value */
  protected value = signal<string>('');

  /** The function to call when the value changes */
  private onChange?: ChangeFn<string>;

  /** The function to call when the control is touched */
  protected onTouched?: TouchedFn;

  writeValue(value: string): void {
    this.value.set(value);
  }

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

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

  protected onValueChange(event: Event): void {
    const input = event.target as HTMLInputElement;
    this.value.set(input.value);
    this.onChange?.(input.value);
  }

  protected onFocusOut(event: FocusEvent): void {
    // only mark as touched when focus leaves the component, not when moving
    // between the input and the toggle button
    const host = event.currentTarget as HTMLElement;
    if (!host.contains(event.relatedTarget as Node | null)) {
      this.onTouched?.();
    }
  }
}
