import { getNpub, getUserProfile } from '@/ndk';
import type { NDKUserProfile } from '@nostr-dev-kit/ndk';
import { LitElement, css } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import { map } from 'lit/directives/map.js';
import { html, literal } from 'lit/static-html.js';

import '@components/AppGrid';
import '@components/General/Card';
import '@components/NostrAvatar';
import '@components/Widgets/BitcoinBlockWidget';
import '@components/Widgets/WalletWidget';

@customElement('arx-eve-home')
export class Home extends LitElement {
  @state()
  private npub: string | undefined;
  @state()
  private profile: NDKUserProfile | undefined;
  @state()
  private username: string | undefined;

  apps = [
    {
      id: 0,
      href: 'letters',
      name: 'Letters',
      color: '#FF3E96',
      icon: 'arx:letters',
    },
    {
      id: 1,
      href: 'howl',
      name: 'Howl',
      color: '#00CD66',
      icon: 'arx:howl',
    },
    {
      id: 2,
      href: 'calendar',
      name: 'Calendar',
      color: '#FF8C00',
      icon: 'arx:calendar',
    },
    {
      id: 3,
      href: 'arbor',
      name: 'Arbor',
      color: '#FF4040',
      icon: 'arx:arbor',
    },
    {
      id: 5,
      href: 'market',
      name: 'Market',
      color: '#9370DB',
      icon: 'arx:market',
    },
    {
      id: 6,
      href: 'wallet',
      name: 'Wallet',
      color: '#1E90FF',
      icon: 'arx:wallet',
    },
    {
      id: 7,
      href: 'pool',
      name: 'Pool',
      color: '#7aD700',
      icon: 'arx:pool',
    },
    {
      id: 8,
      href: 'settings',
      name: 'Settings',
      color: '#7B68EE',
      icon: 'arx:settings',
    },
    },
  ];

  widgets = [
    {
      title: 'Bitcoin Block',
      content: literal`arx-bitcoin-block-widget`,
    },
    {
      title: 'Bitcoin Wallet',
      content: literal`arx-wallet-widget`,
    },
  ];

  async loadProperties() {
    const npub = await getNpub();
    if (!npub) return alert('No npub?');
    this.npub = npub;
    this.profile = (await getUserProfile(this.npub)) as NDKUserProfile;
    this.username = this.profile?.name || this.npub.substring(0, 8);
  }

  static override styles = [
    css`
      ::-webkit-scrollbar {
        width: 12px;
        height: 12px;
      }

      ::-webkit-scrollbar-track {
        background: var(--color-base-200);
        border-radius: var(--radius-field);
      }

      ::-webkit-scrollbar-thumb {
        background: var(--color-base-300);
        border-radius: var(--radius-field);
        border: 2px solid var(--color-base-200);
        transition: var(--transition);
      }

      ::-webkit-scrollbar-thumb:hover {
        background: var(--color-neutral);
      }

      .content-wrapper {
        margin: auto;
        height: 90vh;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 20px;
        width: 100%;
        max-width: 1200px;
      }

      .home-container {
        flex: 1;
        padding: 0;
        height: auto;
      }

      .widgets-container {
        height: 75vh;
        overflow: auto;
        width: 350px;
        margin: 0 auto;
      }

      @media (max-width: 1024px) {
        .content-wrapper {
          gap: 0;
          height: auto;
          flex-direction: column;
        }

        .home-container {
          width: 100%;
        }

        .widgets-container {
          overflow: unset;
          height: auto;
          width: calc(100vw - 40px);
          padding-bottom: 10px;
        }
      }

      .welcome-section {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 20px;
        padding: 20px;
        border-radius: 20px;
      }

      .welcome-text h1 {
        margin: 0;
        font-size: 24px;
      }
    `,
  ];

  override connectedCallback() {
    super.connectedCallback();
    this.loadProperties();
  }

  override render() {
    return html`
      <div class="content-wrapper">
        <arx-card class="home-container">
          <arx-card class="welcome-section">
            <arx-nostr-avatar
              .profile=${this.profile as NDKUserProfile}
              size="huge"
            ></arx-nostr-avatar>
            <div class="welcome-text">
              <h1>Welcome, ${this.username}</h1>
            </div>
          </arx-card>
          <arx-app-grid .apps=${this.apps}></arx-app-grid>
        </arx-card>
        <div class="widgets-container">
          ${map(this.widgets, (widget) => html`<arx-card><${widget.content}></${widget.content}></arx-card>`)}
        </div>
      </div>
    `;
  }
}
