import {css, styled, Theme} from '@mui/material/styles';
import {rgba} from 'polished';
import {
    IEnterCode,
    IEnterCodeDescription,
    IEnterCodeWrapper,
} from '../enter-code.type';
import {
    EnterCodeWrapperSizeStyle as wrapperSizes,
    EnterCodeSizeStyle as sizes,
} from './enter-code.size.style';
import {themeDetector} from '../../../../../helpers/theme-detector/theme-detector';

const Container = styled('div')``;

const Input = styled('input')<Omit<IEnterCode, 'onComplete' | 'fields'>>`
    ${({size}) => sizes[size || 'md']};
    font-family: inherit;
    border-radius: 8px;
    color: ${({theme, error, color}) =>
        theme.palette[themeDetector(error, color)][600]};
    border: 1px solid
        ${({theme, error, color}) =>
            theme.palette[themeDetector(error, color)][300]};
    outline: none !important;
    font-weight: 500;
    text-align: center;
    box-shadow: 0px 1px 2px 0px
        ${({theme}) => rgba(theme.palette.shadow[500] as string, 0.05)};

    /* Chrome, Safari, Edge, Opera */
    ::-webkit-outer-spin-button,
    ::-webkit-inner-spin-button {
        -webkit-appearance: none;
        margin: 0;
    }

    &:focus {
        box-shadow:
            0px 0px 0px 4px
                ${({theme, error, color}) =>
                    theme.palette[themeDetector(error, color)][100]},
            0px 1px 2px 0px
                ${({theme}) => rgba(theme.palette.gray[900] as string, 0.5)};
        border: 1px solid
            ${({theme, error, color}) =>
                theme.palette[themeDetector(error, color)][300]};
    }

    &:not([value='']) {
        border: 1px solid
            ${({theme, error, color}) =>
                theme.palette[themeDetector(error, color)][300]};
    }

    &:disabled {
        color: ${({theme}) => theme.palette.typography[200]};
        border: 1px solid ${({theme}) => theme.palette['border'][300]};
    }
`;

const Wrapper = styled('div')<IEnterCodeWrapper>`
    display: flex;
    direction: ltr;
    ${({size}) => wrapperSizes[size || 'md']};
`;

const Label = styled('label')`
    color: ${({theme}) => theme.palette.typography[700]};
    font-family: inherit;
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.25rem;
    margin-bottom: 6px;
    display: inline-block;
`;

const Description = styled('span')<IEnterCodeDescription>`
    color: ${({theme, error}) =>
        theme.palette[error ? 'error' : 'typography'][500]};
    font-family: inherit;
    font-size: 0.875rem;
    font-weight: 400;
    line-height: 1.25rem;
    margin-top: 6px;
    display: inline-block;
`;

// defined placeholder style here because of stylis issue
// https://github.com/thysultan/stylis/issues/337
const placeholder = (theme: Theme) => css`
    &::placeholder {
        color: ${theme.palette.typography[300]};
    }
`;

export const EnterCodeStyle = {
    Input,
    Container,
    Label,
    Description,
    Wrapper,
    placeholder,
};
