import {
  DeesElement,
  customElement,
  html,
  css,
  cssManager,
  property,
  type TemplateResult,
} from '@design.estate/dees-element';

declare global {
  interface HTMLElementTagNameMap {
    'sz-login-view': SzLoginView;
  }
}

@customElement('sz-login-view')
export class SzLoginView extends DeesElement {
  public static demo = () => html`
    <div style="height: 600px; display: flex; align-items: center; justify-content: center; background: #09090b;">
      <sz-login-view></sz-login-view>
    </div>
  `;

  @property({ type: Boolean })
  public accessor loading: boolean = false;

  @property({ type: String })
  public accessor error: string = '';

  @property({ type: String })
  public accessor title: string = 'serve.zone';

  @property({ type: String })
  public accessor subtitle: string = 'Sign in to your onebox';

  public static styles = [
    cssManager.defaultStyles,
    css`
      :host {
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 100vh;
        width: 100%;
        background: ${cssManager.bdTheme('#f4f4f5', '#09090b')};
      }

      .login-container {
        width: 100%;
        max-width: 400px;
        padding: 24px;
      }

      .login-card {
        background: ${cssManager.bdTheme('#ffffff', '#18181b')};
        border: 1px solid ${cssManager.bdTheme('#e4e4e7', '#27272a')};
        border-radius: 12px;
        padding: 32px;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
      }

      .logo-section {
        text-align: center;
        margin-bottom: 32px;
      }

      .logo {
        width: 64px;
        height: 64px;
        background: ${cssManager.bdTheme('#18181b', '#fafafa')};
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 16px;
      }

      .logo svg {
        width: 36px;
        height: 36px;
        color: ${cssManager.bdTheme('#fafafa', '#18181b')};
      }

      .title {
        font-size: 24px;
        font-weight: 700;
        color: ${cssManager.bdTheme('#18181b', '#fafafa')};
        margin-bottom: 4px;
      }

      .subtitle {
        font-size: 14px;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
      }

      .form {
        display: flex;
        flex-direction: column;
        gap: 20px;
      }

      .form-group {
        display: flex;
        flex-direction: column;
        gap: 6px;
      }

      .form-label {
        font-size: 14px;
        font-weight: 500;
        color: ${cssManager.bdTheme('#18181b', '#fafafa')};
      }

      .form-input {
        width: 100%;
        padding: 12px 14px;
        background: ${cssManager.bdTheme('#ffffff', '#09090b')};
        border: 1px solid ${cssManager.bdTheme('#e4e4e7', '#27272a')};
        border-radius: 8px;
        font-size: 14px;
        color: ${cssManager.bdTheme('#18181b', '#fafafa')};
        outline: none;
        transition: border-color 200ms ease, box-shadow 200ms ease;
        box-sizing: border-box;
      }

      .form-input:focus {
        border-color: ${cssManager.bdTheme('#3b82f6', '#60a5fa')};
        box-shadow: 0 0 0 3px ${cssManager.bdTheme('rgba(59, 130, 246, 0.1)', 'rgba(96, 165, 250, 0.1)')};
      }

      .form-input::placeholder {
        color: ${cssManager.bdTheme('#a1a1aa', '#52525b')};
      }

      .form-input.error {
        border-color: ${cssManager.bdTheme('#ef4444', '#f87171')};
      }

      .error-message {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 12px 14px;
        background: ${cssManager.bdTheme('#fef2f2', 'rgba(239, 68, 68, 0.1)')};
        border: 1px solid ${cssManager.bdTheme('#fecaca', 'rgba(239, 68, 68, 0.2)')};
        border-radius: 8px;
        font-size: 14px;
        color: ${cssManager.bdTheme('#dc2626', '#f87171')};
      }

      .error-message svg {
        width: 18px;
        height: 18px;
        flex-shrink: 0;
      }

      .submit-button {
        width: 100%;
        padding: 12px 20px;
        background: ${cssManager.bdTheme('#18181b', '#fafafa')};
        border: none;
        border-radius: 8px;
        font-size: 14px;
        font-weight: 600;
        color: ${cssManager.bdTheme('#fafafa', '#18181b')};
        cursor: pointer;
        transition: opacity 200ms ease, transform 200ms ease;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
      }

      .submit-button:hover:not(:disabled) {
        opacity: 0.9;
      }

      .submit-button:active:not(:disabled) {
        transform: scale(0.98);
      }

      .submit-button:disabled {
        opacity: 0.6;
        cursor: not-allowed;
      }

      .spinner {
        width: 18px;
        height: 18px;
        border: 2px solid transparent;
        border-top-color: currentColor;
        border-radius: 50%;
        animation: spin 0.8s linear infinite;
      }

      @keyframes spin {
        to {
          transform: rotate(360deg);
        }
      }

      .footer {
        margin-top: 24px;
        text-align: center;
        font-size: 13px;
        color: ${cssManager.bdTheme('#71717a', '#a1a1aa')};
      }

      .footer a {
        color: ${cssManager.bdTheme('#3b82f6', '#60a5fa')};
        text-decoration: none;
      }

      .footer a:hover {
        text-decoration: underline;
      }
    `,
  ];

  public render(): TemplateResult {
    return html`
      <div class="login-container">
        <div class="login-card">
          <div class="logo-section">
            <div class="logo">
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"></path>
                <polyline points="3.27 6.96 12 12.01 20.73 6.96"></polyline>
                <line x1="12" y1="22.08" x2="12" y2="12"></line>
              </svg>
            </div>
            <div class="title">${this.title}</div>
            <div class="subtitle">${this.subtitle}</div>
          </div>

          <form class="form" @submit=${this.handleSubmit}>
            ${this.error ? html`
              <div class="error-message">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                  <circle cx="12" cy="12" r="10"></circle>
                  <line x1="12" y1="8" x2="12" y2="12"></line>
                  <line x1="12" y1="16" x2="12.01" y2="16"></line>
                </svg>
                ${this.error}
              </div>
            ` : ''}

            <div class="form-group">
              <label class="form-label" for="username">Username</label>
              <input
                type="text"
                id="username"
                class="form-input ${this.error ? 'error' : ''}"
                placeholder="Enter your username"
                autocomplete="username"
                ?disabled=${this.loading}
                required
              >
            </div>

            <div class="form-group">
              <label class="form-label" for="password">Password</label>
              <input
                type="password"
                id="password"
                class="form-input ${this.error ? 'error' : ''}"
                placeholder="Enter your password"
                autocomplete="current-password"
                ?disabled=${this.loading}
                required
              >
            </div>

            <button type="submit" class="submit-button" ?disabled=${this.loading}>
              ${this.loading ? html`
                <div class="spinner"></div>
                Signing in...
              ` : 'Sign in'}
            </button>
          </form>

          <div class="footer">
            Powered by <a href="https://serve.zone" target="_blank">serve.zone</a>
          </div>
        </div>
      </div>
    `;
  }

  private handleSubmit(e: Event) {
    e.preventDefault();

    const usernameInput = this.shadowRoot?.getElementById('username') as HTMLInputElement;
    const passwordInput = this.shadowRoot?.getElementById('password') as HTMLInputElement;

    if (!usernameInput || !passwordInput) return;

    const username = usernameInput.value.trim();
    const password = passwordInput.value;

    if (!username || !password) {
      this.error = 'Please enter both username and password';
      return;
    }

    this.error = '';
    this.dispatchEvent(new CustomEvent('login', {
      detail: { username, password },
      bubbles: true,
      composed: true,
    }));
  }

  public clearForm() {
    const usernameInput = this.shadowRoot?.getElementById('username') as HTMLInputElement;
    const passwordInput = this.shadowRoot?.getElementById('password') as HTMLInputElement;

    if (usernameInput) usernameInput.value = '';
    if (passwordInput) passwordInput.value = '';
    this.error = '';
  }

  public focusUsername() {
    const usernameInput = this.shadowRoot?.getElementById('username') as HTMLInputElement;
    if (usernameInput) usernameInput.focus();
  }
}
