<script lang="ts">
  import { createEventDispatcher, onMount } from "svelte";
  import gsap from "gsap";

  export let value: string = "";
  export let placeholder: string = "Type a message...";
  export let disabled: boolean = false;
  export let processing: boolean = false;

  const dispatch = createEventDispatcher();

  let textareaRef: HTMLTextAreaElement;
  let sendButtonRef: HTMLButtonElement;
  let stopButtonRef: HTMLButtonElement;
  let inputAreaRef: HTMLDivElement;

  function handleSubmit() {
    if (value.trim() && !disabled && !processing) {
      const tl = gsap.timeline();

      if (sendButtonRef) {
        tl.to(sendButtonRef, {
          scale: 0.85,
          duration: 0.1,
          ease: "power2.in"
        })
        .to(sendButtonRef, {
          scale: 1.1,
          rotation: 360,
          duration: 0.4,
          ease: "back.out(2)"
        })
        .to(sendButtonRef, {
          scale: 1,
          duration: 0.2,
          ease: "power2.out"
        }, "-=0.1");
      }

      if (textareaRef) {
        tl.to(textareaRef,
          {
            borderColor: "rgba(76, 175, 80, 0.3)",
            backgroundColor: "rgba(76, 175, 80, 0.02)",
            duration: 0.2
          },
          0
        )
        .to(textareaRef,
          {
            borderColor: "rgba(139, 115, 85, 0.08)",
            backgroundColor: "rgba(139, 115, 85, 0.03)",
            duration: 0.3,
            ease: "power2.out"
          }
        );
      }

      dispatch("send", value.trim());
      value = "";
    }
  }

  function handleStop() {
    if (processing) {
      if (stopButtonRef) {
        gsap.to(stopButtonRef, {
          scale: 0.8,
          duration: 0.15,
          yoyo: true,
          repeat: 1,
          ease: "power2.inOut"
        });
      }
      dispatch("stop");
    }
  }

  let isTyping = false;

  onMount(() => {
    if (inputAreaRef) {
      gsap.fromTo(inputAreaRef,
        { opacity: 0, y: 20 },
        { opacity: 1, y: 0, duration: 0.4, ease: "power2.out" }
      );
    }
  });

  $: if (processing && stopButtonRef) {
    gsap.to(stopButtonRef, {
      scale: 1.05,
      duration: 0.8,
      yoyo: true,
      repeat: -1,
      ease: "power2.inOut"
    });
  }

  $: if (!processing && stopButtonRef) {
    gsap.killTweensOf(stopButtonRef);
    gsap.set(stopButtonRef, { scale: 1 });
  }

  $: if (value.length > 0 && !isTyping) {
    isTyping = true;
    if (sendButtonRef) {
      gsap.to(sendButtonRef, {
        scale: 1.05,
        backgroundColor: "rgba(124, 152, 133, 0.12)",
        duration: 0.3,
        ease: "power2.out"
      });
    }
  }

  $: if (value.length === 0 && isTyping) {
    isTyping = false;
    if (sendButtonRef) {
      gsap.to(sendButtonRef, {
        scale: 1,
        backgroundColor: "rgba(124, 152, 133, 0.08)",
        duration: 0.3,
        ease: "power2.out"
      });
    }
  }

  function handleKeydown(event: KeyboardEvent) {
    if (event.key === "Enter" && !event.shiftKey) {
      event.preventDefault();
      handleSubmit();
    }
  }
</script>

<div class="input-area" bind:this={inputAreaRef}>
  <div class="input-wrapper">
    <textarea
      bind:this={textareaRef}
      bind:value
      on:keydown={handleKeydown}
      {placeholder}
      {disabled}
      rows="1"
    />
    <div class="input-glow"></div>
  </div>
  {#if processing}
    <button
      bind:this={stopButtonRef}
      on:click={handleStop}
      class="stop-btn"
      title="Stop conversation"
    >
      <svg width="12" height="12" viewBox="0 0 12 12" fill="currentColor">
        <rect x="2" y="2" width="8" height="8" rx="1"/>
      </svg>
    </button>
  {:else}
    <button
      bind:this={sendButtonRef}
      on:click={handleSubmit}
      disabled={disabled || !value.trim()}
      class="send-btn"
      title="Send message"
    >
      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <path d="M22 2L11 13M22 2L15 22L11 13M22 2L2 9L11 13"/>
      </svg>
    </button>
  {/if}
</div>

<style>
  .input-area {
    display: flex;
    gap: 0.5rem;
    padding: 12px 16px;
    background: rgba(20, 19, 17, 0.5);
    border-top: 1px solid rgba(139, 115, 85, 0.18);
  }

  .input-wrapper {
    flex: 1;
    position: relative;
    border-radius: 4px;
    overflow: hidden;
  }

  .input-glow {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(45deg, rgba(124, 152, 133, 0.05), rgba(139, 115, 85, 0.02));
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
    border-radius: 4px;
  }

  textarea {
    width: 100%;
    background: rgba(30, 28, 26, 0.5);
    color: rgba(251, 248, 244, 0.95);
    border: 1px solid rgba(139, 115, 85, 0.15);
    border-radius: 4px;
    padding: 0.4rem 0.6rem;
    resize: none;
    font-family: "JetBrains Mono", "Monaco", "Menlo", monospace;
    font-size: 11px;
    line-height: 1.6;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    z-index: 1;
  }

  textarea:focus {
    outline: none;
    border-color: rgba(124, 152, 133, 0.3);
    background: rgba(35, 33, 30, 0.6);
    box-shadow: 0 0 0 1px rgba(124, 152, 133, 0.15);
  }

  textarea:focus + .input-glow {
    opacity: 1;
  }

  textarea:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  .send-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(124, 152, 133, 0.12);
    color: rgba(124, 152, 133, 0.9);
    border: 1px solid rgba(124, 152, 133, 0.2);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;
  }

  .send-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s ease;
  }

  .send-btn:hover:not(:disabled) {
    background: rgba(124, 152, 133, 0.2);
    border-color: rgba(124, 152, 133, 0.35);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(124, 152, 133, 0.25);
    color: rgba(124, 152, 133, 1);
  }

  .send-btn:hover:not(:disabled)::before {
    left: 100%;
  }

  .send-btn:active:not(:disabled) {
    transform: translateY(0) scale(0.98);
  }

  .send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
  }

  .stop-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(184, 84, 80, 0.12);
    color: rgba(184, 84, 80, 0.9);
    border: 1px solid rgba(184, 84, 80, 0.2);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;
  }

  .stop-btn::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    border: 2px solid rgba(244, 67, 54, 0.3);
    border-radius: 50%;
    transform: translate(-50%, -50%);
    animation: stop-pulse 1.5s ease-in-out infinite;
  }

  @keyframes stop-pulse {
    0%, 100% {
      transform: translate(-50%, -50%) scale(1);
      opacity: 1;
    }
    50% {
      transform: translate(-50%, -50%) scale(1.5);
      opacity: 0.3;
    }
  }

  .stop-btn:hover {
    background: rgba(184, 84, 80, 0.2);
    border-color: rgba(184, 84, 80, 0.35);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(184, 84, 80, 0.25);
    color: rgba(184, 84, 80, 1);
  }

  .stop-btn:active {
    transform: translateY(0) scale(0.98);
  }
</style>