import re
from datetime import date, datetime
from typing import List, Optional, Type, Union, Dict, Tuple

from ...model import (
    PathParameterInEnum, SchemaPropertyFormatEnum, SchemaPropertyTypeEnum
)
from ...utils import (
    is_type
)

URL_PATH_PARAMETER_PATTERN = re.compile(r"{(.*?)}")
INTERACTOR_REPLACE_PATTERN = re.compile("Interactor", re.IGNORECASE)
OPENAPI_SCALAR_TYPES_DEFINITIONS: Dict[Type, Tuple] = {
    int: (SchemaPropertyTypeEnum.integer, None),
    float: (SchemaPropertyTypeEnum.number, None),
    bool: (SchemaPropertyTypeEnum.boolean, None),
    # String data
    str: (SchemaPropertyTypeEnum.string, None),
    date: (SchemaPropertyTypeEnum.string, SchemaPropertyFormatEnum.date),
    datetime: (SchemaPropertyTypeEnum.string, SchemaPropertyFormatEnum.datetime),
}

PARAMETER_LOCATION_MAP = {
    "header": PathParameterInEnum.header,
    "query": PathParameterInEnum.query,
    "path": PathParameterInEnum.path,
}

DEFAULT_RESPONSE_DESCRIPTION = "Response description"
DEFAULT_CONTENT_TYPE = "application/json"
DEFAULT_SUCCESS_CODE = 200


def get_path_parameters(url: str) -> List[str]:
    return URL_PATH_PARAMETER_PATTERN.findall(url)


def get_interactor_description(value: Optional[Union[str, Type]]) -> Optional[str]:
    if value is None:
        return None
    name = value
    if is_type(value):
        name = getattr(value, "__name__")
    name = INTERACTOR_REPLACE_PATTERN.sub("", str(name))
    out = ""
    for i in range(len(name)):
        char = name[i]
        if char.isupper() and len(out) > 0:
            out += " "
        out += char
    return out
