import React from 'react';
import styled from 'styled-components';

export interface ISimpleProgressBarProps {
  value: number;
  bgColor?: string;
  margin?: number;
}

export const SimpleProgressBar: React.FC<ISimpleProgressBarProps> = ({
  value,
  bgColor = 'red',
  margin = 20,
}) => {
  // Ensure the width is at least 1% if value is greater than 0
  const width = value > 0 ? Math.max(1, Math.min(100, value)) : 0;

  return (
    <Container className="simple-progress-bar">
      <ProgressBarContainer margin={margin}>
        <BackgroundBar>
          <Progress width={width} bgColor={bgColor} />
        </BackgroundBar>
      </ProgressBarContainer>
    </Container>
  );
};

const Container = styled.div`
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
`;

const BackgroundBar = styled.span`
  background-color: rgba(0, 0, 0, 0.075);
`;

interface IProgressProps {
  width: number;
  bgColor: string;
}

const Progress = styled.span<IProgressProps>`
  background-color: ${props => props.bgColor};
  width: ${props => props.width.toFixed(2)}%;
  transition: width 0.5s ease-in-out;
`;

interface IProgressBarContainerProps {
  margin: number;
}

const ProgressBarContainer = styled.div<IProgressBarContainerProps>`
  border-radius: 60px;
  border: 1px solid #282424;
  overflow: hidden;
  width: 100%;
  span {
    display: block;
    height: 100%;
  }
  height: 8px;
  margin-left: ${props => props.margin}px;
`;
