import {styled, Theme} from '@mui/material';
import {rgba} from 'polished';
import {IInputContainer, IInputDescription, IInputField} from './input.type';
import {themeDetector} from '../../../../helpers/theme-detector/theme-detector';
import {css} from '@emotion/react';

const Container = styled('div')<IInputContainer>`
    width: 100%;
    display: flex;
    border-radius: 8px;
    box-shadow: 0px 1px 2px 0px
        ${({theme}) => rgba(theme.palette.shadow[500] as string, 0.05)};
    border: 1px solid ${({theme}) => theme.palette.border[300]};
    height: 40px;
    font-family: inherit;

    &.focused {
        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-color: ${({theme, error, color}) =>
            theme.palette[themeDetector(error, color)][300]};
    }

    &.error {
        border-color: ${({theme}) => theme.palette.error[300]};
    }

    &.disabled {
        background-color: ${({theme}) => theme.palette.gray[50]};

        border-color: ${({theme}) => theme.palette.border[300]};

        & .field {
            -webkit-text-fill-color: ${({theme}) =>
                theme.palette.typography[300]};
        }
    }
`;

const Field = styled('input')<IInputField>`
    color: ${({theme}) => theme.palette.typography[900]};
    outline: none !important;
    border: none;
    padding: 8.5px 12px;
    width: 100%;
    height: 100%;
    border-radius: 8px;
    background-color: transparent;
    font-family: inherit;
    font-size: 1rem;
    text-align: start;

    &[type='number']::-webkit-inner-spin-button,
    &[type='number']::-webkit-outer-spin-button {
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        margin: 0;
    }
`;

const Wrapper = styled('div')`
    display: flex;
    flex-direction: column;
    align-items: flex-start;
`;

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

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

const StartAdornment = styled('div')`
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
`;

const EndAdornment = styled('div')`
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
`;

const Header = styled('div')`
    display: flex;
    gap: 4px;
`;

// 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 InputStyle = {
    Container,
    Field,
    Wrapper,
    Label,
    Description,
    StartAdornment,
    EndAdornment,
    Header,
    placeholder,
};
