import { useCallback, useEffect, useRef, useState } from "react";
import styled, { keyframes } from "styled-components";
import {
  appear,
  defaultBorderRadius,
  secondaryDark,
  vscBackground,
  vscBackgroundTransparent,
} from ".";
import {
  ChevronDown,
  ChevronRight,
  XMark,
  ArrowPath,
} from "@styled-icons/heroicons-outline";
import { HistoryNode } from "../../../schema/HistoryNode";
import ReactMarkdown from "react-markdown";
import HeaderButtonWithText from "./HeaderButtonWithText";
import CodeBlock from "./CodeBlock";

interface StepContainerProps {
  historyNode: HistoryNode;
  onReverse: () => void;
  inFuture: boolean;
  onRefinement: (input: string) => void;
  onUserInput: (input: string) => void;
  onRetry: () => void;
  onDelete: () => void;
  open: boolean;
  onToggleAll: () => void;
  onToggle: () => void;
  isFirst: boolean;
  isLast: boolean;
}

const MainDiv = styled.div<{ stepDepth: number; inFuture: boolean }>`
  opacity: ${(props) => (props.inFuture ? 0.3 : 1)};
  animation: ${appear} 0.3s ease-in-out;
  /* padding-left: ${(props) => props.stepDepth * 20}px; */
  overflow: hidden;
  margin-left: 0px;
  margin-right: 0px;
`;

const StepContainerDiv = styled.div<{ open: boolean }>`
  /* background-color: ${(props) =>
    props.open ? vscBackground : secondaryDark}; */
  /* border-radius: ${defaultBorderRadius}; */
  /* padding: 8px; */
`;

const HeaderDiv = styled.div<{ error: boolean }>`
  background-color: ${(props) =>
    props.error ? "#522" : vscBackgroundTransparent};
  display: grid;
  grid-template-columns: 1fr auto auto;
  grid-gap: 8px;
  align-items: center;
  padding-right: 8px;
`;

const ContentDiv = styled.div`
  padding: 8px;
  padding-left: 16px;
`;

const MarkdownPre = styled.pre`
  background-color: ${secondaryDark};
  padding: 10px;
  border-radius: ${defaultBorderRadius};
  border: 0.5px solid white;
`;

const StyledCode = styled.code`
  word-wrap: break-word;
  color: lightgray;
`;

const GradientBorder = styled.div<{
  borderWidth?: number;
  borderRadius?: string;
  borderColor?: string;
  isFirst: boolean;
  isLast: boolean;
}>`
  border-radius: ${(props) => props.borderRadius || "0"};
  padding-top: ${(props) =>
    `${(props.borderWidth || 1) / (props.isFirst ? 1 : 2)}px`};
  padding-bottom: ${(props) =>
    `${(props.borderWidth || 1) / (props.isLast ? 1 : 2)}px`};
  background: ${(props) =>
    props.borderColor
      ? props.borderColor
      : `linear-gradient(
    101.79deg,
    #12887a 0%,
    #87245c 37.64%,
    #e12637 65.98%,
    #ffb215 110.45%
  )`};
`;

function StepContainer(props: StepContainerProps) {
  const [isHovered, setIsHovered] = useState(false);
  const naturalLanguageInputRef = useRef<HTMLTextAreaElement>(null);
  const userInputRef = useRef<HTMLInputElement>(null);

  useEffect(() => {
    if (userInputRef?.current) {
      userInputRef.current.focus();
    }
  }, [userInputRef]);

  useEffect(() => {
    if (isHovered) {
      naturalLanguageInputRef.current?.focus();
    }
  }, [isHovered]);

  const onTextInput = useCallback(() => {
    if (naturalLanguageInputRef.current) {
      props.onRefinement(naturalLanguageInputRef.current.value);
      naturalLanguageInputRef.current.value = "";
    }
  }, [naturalLanguageInputRef]);

  return (
    <MainDiv
      stepDepth={(props.historyNode.depth as any) || 0}
      inFuture={props.inFuture}
      onMouseEnter={() => {
        setIsHovered(true);
      }}
      onMouseLeave={() => {
        setIsHovered(false);
      }}
      hidden={props.historyNode.step.hide as any}
    >
      <StepContainerDiv open={props.open}>
        <GradientBorder
          isFirst={props.isFirst}
          isLast={props.isLast}
          borderColor={
            props.historyNode.observation?.error ? "#f00" : undefined
          }
          className="overflow-hidden cursor-pointer"
          onClick={(e) => {
            if (e.metaKey) {
              props.onToggleAll();
            } else {
              props.onToggle();
            }
          }}
        >
          <HeaderDiv
            error={props.historyNode.observation?.error ? true : false}
          >
            <h4 className="m-2">
              {props.open ? (
                <ChevronDown size="1.4em" />
              ) : (
                <ChevronRight size="1.4em" />
              )}
              {props.historyNode.observation?.title ||
                (props.historyNode.step.name as any)}
            </h4>
            {/* <HeaderButton
              onClick={(e) => {
                e.stopPropagation();
                props.onReverse();
              }}
            >
              <Backward size="1.6em" onClick={props.onReverse}></Backward>
            </HeaderButton> */}

            <>
              <HeaderButtonWithText
                onClick={(e) => {
                  e.stopPropagation();
                  props.onDelete();
                }}
                text="Delete"
              >
                <XMark size="1.6em" onClick={props.onDelete} />
              </HeaderButtonWithText>
              {props.historyNode.observation?.error ? (
                <HeaderButtonWithText
                  text="Retry"
                  onClick={(e) => {
                    e.stopPropagation();
                    props.onRetry();
                  }}
                >
                  <ArrowPath size="1.6em" onClick={props.onRetry} />
                </HeaderButtonWithText>
              ) : (
                <></>
              )}
            </>
          </HeaderDiv>
        </GradientBorder>
        <ContentDiv hidden={!props.open}>
          {props.open && false && (
            <>
              <pre className="overflow-x-scroll">
                Step Details:
                <br />
                {JSON.stringify(props.historyNode.step, null, 2)}
              </pre>
            </>
          )}

          {props.historyNode.observation?.error ? (
            <pre className="overflow-x-scroll">
              {props.historyNode.observation.error as string}
            </pre>
          ) : (
            <ReactMarkdown
              key={1}
              className="overflow-x-scroll"
              components={{
                pre: ({ node, ...props }) => {
                  return (
                    <CodeBlock
                      children={props.children[0] as string}
                    ></CodeBlock>
                  );
                },
                code: ({ node, ...props }) => {
                  return (
                    <StyledCode
                      children={props.children[0] as string}
                    ></StyledCode>
                  );
                },
              }}
            >
              {props.historyNode.step.description as any}
            </ReactMarkdown>
          )}
        </ContentDiv>
      </StepContainerDiv>
    </MainDiv>
  );
}

export default StepContainer;
