import { html, nothing, TemplateResult } from 'lit'
import { live } from 'lit/directives/live.js'
import type { INtlmAuthorization } from '@api-client/core/models/Authorization.js'
import { Authorization } from './Authorization.js'
import Input from '../../../md/input/Input.js'
import '../../../md/text-field/ui-filled-text-field.js'

export default class NtlmAuthorization extends Authorization {
  override checkValidity(): boolean {
    const { authorization } = this
    if (!authorization) {
      return true
    }

    const config = authorization.config as INtlmAuthorization
    return !!config && !!config.username
  }

  protected handleUsername(e: Event): void {
    const { authorization } = this
    if (!authorization) {
      return
    }
    const input = e.target as Input
    const { value } = input
    if (!authorization.config) {
      authorization.config = {} as INtlmAuthorization
    }
    ;(authorization.config as INtlmAuthorization).username = value
    this.notifyChange()
  }

  protected handlePassword(e: Event): void {
    const { authorization } = this
    if (!authorization) {
      return
    }
    const input = e.target as Input
    const { value } = input
    if (!authorization.config) {
      authorization.config = {} as INtlmAuthorization
    }
    ;(authorization.config as INtlmAuthorization).password = value
    this.notifyChange()
  }

  protected handleDomain(e: Event): void {
    const { authorization } = this
    if (!authorization) {
      return
    }
    const input = e.target as Input
    const { value } = input
    if (!authorization.config) {
      authorization.config = {} as INtlmAuthorization
    }
    ;(authorization.config as INtlmAuthorization).domain = value
    this.notifyChange()
  }

  override render(): TemplateResult {
    return html` ${this.renderHeader()} ${this.renderEditor()} `
  }

  protected renderEditor(): TemplateResult | typeof nothing {
    const { authorization } = this
    if (!authorization) {
      return nothing
    }
    const config = (authorization.config as INtlmAuthorization) || ({} as INtlmAuthorization)
    return html`
      <form autocomplete="on" class="auth-form basic-auth">
        ${this.renderUsername(config)} ${this.renderPassword(config)} ${this.renderDomain(config)}
      </form>
      ${this.renderAuthInfo()}
    `
  }

  protected renderUsername(config: INtlmAuthorization): TemplateResult {
    return html`
      <div class="full-input">
        <ui-filled-text-field
          label="Username"
          name="username"
          .value="${live(config.username || '')}"
          required
          invalidText="Username is required"
          ?disabled="${this.disabled}"
          ?readOnly="${this.readOnly}"
          @change="${this.handleUsername}"
        ></ui-filled-text-field>
      </div>
    `
  }

  protected renderPassword(config: INtlmAuthorization): TemplateResult {
    return html`
      <div class="full-input">
        <ui-filled-text-field
          label="Password"
          name="password"
          type="password"
          .value="${live(config.password || '')}"
          ?disabled="${this.disabled}"
          ?readOnly="${this.readOnly}"
          @change="${this.handlePassword}"
        ></ui-filled-text-field>
      </div>
    `
  }

  protected renderDomain(config: INtlmAuthorization): TemplateResult {
    return html`
      <div class="full-input">
        <ui-filled-text-field
          label="NT Domain"
          name="domain"
          .value="${live(config.domain || '')}"
          ?disabled="${this.disabled}"
          ?readOnly="${this.readOnly}"
          @change="${this.handleDomain}"
        ></ui-filled-text-field>
      </div>
    `
  }
}
