import { IQuest, QuestStatus } from '@rpg-engine/shared';
import React from 'react';
import styled, { CSSProperties } from 'styled-components';
import { uiColors } from '../../constants/uiColors';

export interface IQuestListProps {
  quests?: IQuest[];
  styles?: {
    container?: CSSProperties;
    card?: CSSProperties;
    label?: CSSProperties;
    value?: CSSProperties;
  };
}

export const QuestList: React.FC<IQuestListProps> = ({ quests, styles }) => {
  return (
    <QuestListContainer style={styles?.container}>
      {quests && quests.length > 0 ? (
        quests.map((quest, i) => (
          <QuestCard key={i} style={styles?.card}>
            <QuestItem>
              <Label style={styles?.label}>Title:</Label>
              <Value style={styles?.value}>
                {formatQuestText(quest.title)}
              </Value>
            </QuestItem>
            <QuestItem>
              <Label style={styles?.label}>Status:</Label>
              <Value
                style={{
                  ...styles?.value,
                  color: getQuestStatusColor(quest.status),
                }}
              >
                {formatQuestStatus(quest.status) ?? 'Unknown'}
              </Value>
            </QuestItem>
            <QuestItem>
              <Label style={styles?.label}>Description:</Label>
              <Value style={styles?.value}>{quest.description}</Value>
            </QuestItem>
          </QuestCard>
        ))
      ) : (
        <NoQuestContainer>
          <p>There are no ongoing quests</p>
        </NoQuestContainer>
      )}
    </QuestListContainer>
  );
};

const QuestListContainer = styled.div`
  max-height: 400px;
  padding: 10px;
  border-radius: 10px;
  font-size: 0.7rem;
`;

const QuestCard = styled.div`
  background-color: ${uiColors.darkGray};
  padding: 15px;
  margin-bottom: 10px;
  border-radius: 10px;
  border: 1px solid ${uiColors.gray};
  display: flex;
  flex-direction: column;
`;

const QuestItem = styled.div`
  display: flex;
  margin-bottom: 5px;
  flex-wrap: wrap;

  &:last-child {
    margin-bottom: 0;
  }
`;

const Label = styled.span`
  font-weight: bold;
  color: ${uiColors.yellow} !important;
  margin-right: 10px;
`;

const Value = styled.span`
  flex-grow: 1;
  color: ${uiColors.white};
  word-wrap: break-word;
`;

const NoQuestContainer = styled.div`
  text-align: center;
  p {
    margin-top: 5px;
    color: ${uiColors.lightGray};
  }
`;

export const formatQuestText = (text: string) => {
  if (!text) return '';
  return text
    .split('-')
    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ');
};

export const getQuestStatusColor = (status?: QuestStatus) => {
  switch (status) {
    case QuestStatus.Pending:
      return uiColors.orange;
    case QuestStatus.InProgress:
      return uiColors.blue;
    case QuestStatus.Completed:
      return uiColors.lightGreen;
    default:
      return uiColors.white;
  }
};

export const formatQuestStatus = (status?: QuestStatus) => {
  if (!status) return '';
  return status
    .split(/(?=[A-Z])/)
    .join(' ')
    .replace(/^\w/, c => c.toUpperCase());
};
