import { Icon, type IconName } from '../ui/Icon';

const AVATAR_TONES = ['bg-[#ec3f20]', 'bg-[#2b3585]', 'bg-[#f59e0b]', 'bg-[#0e7c66]'];

function Line({ className }: { className: string }) {
  return <span className={`block rounded-full ${className}`} />;
}

function AppScreen() {
  return (
    <div className="grid h-full grid-cols-[22%_1fr] overflow-hidden bg-page">
      <div className="flex flex-col gap-2.5 bg-[#020626] p-3">
        <div className="mb-2 flex items-center gap-1.5">
          <span className="h-4 w-4 rounded-[5px] bg-gradient-to-br from-[#ff6b4a] to-[#b31800]" />
          <Line className="h-1.5 w-10 bg-white/50" />
        </div>
        {['w-12', 'w-9', 'w-11', 'w-8', 'w-10'].map((width, index) => (
          <div key={width} className={`flex items-center gap-1.5 rounded-md px-1.5 py-1 ${index === 1 ? 'bg-white/10' : ''}`}>
            <span className="h-2 w-2 shrink-0 rounded-[3px] border border-white/40" />
            <Line className={`h-1 bg-white/35 ${width}`} />
          </div>
        ))}
      </div>
      <div className="flex flex-col gap-2.5 p-3">
        <div className="flex items-center gap-2">
          <Line className="h-2 w-16 bg-ink/70" />
          <span className="ml-auto h-4 w-20 rounded-full border border-line bg-surface" />
          <span className="h-4 w-4 rounded-full bg-[#2b3585]" />
        </div>
        <div className="grid grid-cols-3 gap-2">
          {['bg-[#ec3f20]', 'bg-[#2b3585]', 'bg-[#16a34a]'].map((tone) => (
            <div key={tone} className="rounded-lg border border-line bg-surface p-2">
              <span className={`block h-1.5 w-6 rounded-full ${tone}`} />
              <Line className="mt-2 h-1.5 w-[80%] bg-ink/20" />
              <Line className="mt-1 h-1.5 w-[55%] bg-ink/10" />
            </div>
          ))}
        </div>
        <div className="grid flex-1 grid-cols-3 gap-2">
          {[3, 2, 2].map((count, column) => (
            <div key={column} className="flex flex-col gap-1.5 rounded-lg bg-sunken p-1.5">
              <Line className="h-1 w-8 bg-ink/30" />
              {Array.from({ length: count }, (_, row) => (
                <div key={row} className="rounded-md border border-line bg-surface p-1.5">
                  <Line className="h-1 w-[85%] bg-ink/25" />
                  <Line className="mt-1 h-1 w-[50%] bg-ink/10" />
                  <div className="mt-1.5 flex items-center justify-between">
                    <span className={`h-1.5 w-5 rounded-full ${['bg-[#ff8a6b]/60', 'bg-[#2b3585]/40', 'bg-[#16a34a]/50'][(row + column) % 3]}`} />
                    <span className={`h-2.5 w-2.5 rounded-full ${AVATAR_TONES[(row + column) % 4]}`} />
                  </div>
                </div>
              ))}
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}

function Laptop() {
  return (
    <div className="w-full">
      <div className="rounded-t-[14px] border border-[#1b2140] bg-[#0b1024] p-[1.6%] shadow-[0_40px_80px_-30px_rgba(2,6,38,0.55)]">
        <div className="aspect-[16/10] overflow-hidden rounded-[6px]">
          <AppScreen />
        </div>
      </div>
      <div className="relative mx-[-6%] h-3 rounded-b-[14px] bg-gradient-to-b from-[#d7dbe4] to-[#aeb4c2] shadow-[0_18px_30px_-12px_rgba(2,6,38,0.45)]">
        <span className="absolute top-0 left-1/2 h-1.5 w-[16%] -translate-x-1/2 rounded-b-md bg-[#9aa1b1]" />
      </div>
    </div>
  );
}

function Phone() {
  return (
    <div className="rounded-[26px] border border-[#1b2140] bg-[#0b1024] p-[5px] shadow-[0_30px_60px_-20px_rgba(2,6,38,0.6)]">
      <div className="flex aspect-[9/18] flex-col gap-2 overflow-hidden rounded-[21px] bg-surface p-3 pt-2">
        <span className="mx-auto h-1.5 w-10 rounded-full bg-ink/80" />
        <div className="mt-1 flex items-center justify-between">
          <Line className="h-2 w-12 bg-ink/70" />
          <span className="h-4 w-4 rounded-full bg-[#ec3f20]" />
        </div>
        <div className="rounded-xl bg-gradient-to-br from-[#ff6b4a] to-[#b31800] p-2.5">
          <Line className="h-1.5 w-10 bg-white/80" />
          <Line className="mt-1.5 h-1.5 w-14 bg-white/50" />
        </div>
        {[true, true, false, false].map((done, index) => (
          <div key={index} className="flex items-center gap-2 rounded-lg border border-line px-2 py-1.5">
            <span className={`grid h-3 w-3 place-items-center rounded-full ${done ? 'bg-[#16a34a]' : 'border border-line-2'}`} />
            <Line className={`h-1 ${done ? 'w-10 bg-ink/20' : 'w-12 bg-ink/35'}`} />
          </div>
        ))}
        <span className="mt-auto h-5 rounded-lg bg-ink/90" />
      </div>
    </div>
  );
}

function ToastCard({ icon, tone, title, className }: { icon: IconName; tone: string; title: string; className: string }) {
  return (
    <div className={`tcx-glass absolute flex items-center gap-2.5 rounded-2xl text-ink py-2.5 pr-4 pl-2.5 ${className}`}>
      <span className={`grid h-8 w-8 shrink-0 place-items-center rounded-xl text-white ${tone}`}>
        <Icon name={icon} size={15} strokeWidth={2.2} />
      </span>
      <span className="text-[13px] font-semibold whitespace-nowrap">{title}</span>
    </div>
  );
}

function CollaboratorsCard({ className }: { className: string }) {
  return (
    <div className={`tcx-glass absolute flex items-center gap-3 rounded-2xl text-ink px-3 py-2.5 ${className}`}>
      <div className="flex -space-x-2">
        {AVATAR_TONES.slice(0, 3).map((tone) => (
          <span key={tone} className={`h-7 w-7 rounded-full border-2 border-white/90 ${tone}`} />
        ))}
      </div>
      <span className="text-[13px] font-semibold whitespace-nowrap">Online meetings</span>
    </div>
  );
}

function ToggleCard({ className }: { className: string }) {
  return (
    <div className={`tcx-glass absolute flex items-center gap-4 rounded-2xl px-4 py-3 ${className}`}>
      <div className="flex flex-col gap-1.5">
        <Line className="h-1.5 w-16 bg-ink/50" />
        <Line className="h-1.5 w-10 bg-ink/20" />
      </div>
      <span className="relative h-5 w-9 rounded-full bg-[#16a34a]">
        <span className="absolute top-0.5 right-0.5 h-4 w-4 rounded-full bg-white shadow" />
      </span>
    </div>
  );
}

export function BrandArtwork({ variant = 'hero' }: { variant?: 'hero' | 'auth' | 'minimal' }) {
  const isHero = variant === 'hero';
  const showChips = variant !== 'minimal';

  return (
    <div aria-hidden="true" className="relative mx-auto aspect-[1.08/1] w-full max-w-[600px] select-none">
      <div className="tcx-glow absolute inset-[10%] rounded-[40%]" />
      <div className="tcx-float absolute top-[14%] left-[3%] w-[86%]">
        <Laptop />
      </div>
      <div className="tcx-float-alt absolute right-[1%] bottom-[2%] w-[24%]" style={{ animationDelay: '-2s' }}>
        <Phone />
      </div>

      {showChips ? (
        <>
          <div className="tcx-float-alt absolute inset-0" style={{ animationDelay: '-1s' }}>
            <ToastCard icon="checkCircle" tone="bg-[#16a34a]" title="Task completed" className="top-[3%] left-[0%]" />
          </div>
          <div className="tcx-float absolute inset-0" style={{ animationDelay: '-3s' }}>
            <CollaboratorsCard className="top-[6%] right-[0%]" />
          </div>
          {isHero ? (
            <>
              <div className="tcx-float absolute inset-0" style={{ animationDelay: '-4.5s' }}>
                <ToastCard icon="note" tone="bg-[#2b3585]" title="New comment on your update" className="bottom-[8%] left-[-2%]" />
              </div>
              <div className="tcx-float-alt absolute inset-0" style={{ animationDelay: '-5.5s' }}>
                <ToggleCard className="top-[46%] left-[-5%]" />
              </div>
            </>
          ) : (
            <div className="tcx-float absolute inset-0" style={{ animationDelay: '-4.5s' }}>
              <ToastCard icon="lock" tone="bg-[#ec3f20]" title="Signed in securely" className="bottom-[8%] left-[0%]" />
            </div>
          )}
        </>
      ) : null}
    </div>
  );
}
