def get_all_chat_ids():
    try:
        new_conn = sql.DataBase(sql.user, sql.password, sql.host, sql.database)
        return new_conn.get_chat_ids()

    except Exception:
        return []
def check_notification(chat_id):
    """

    :param chat_id: telegram chat id
    :return: dict or Exception
    """
    try:
        new_conn = sql.DataBase(sql.user, sql.password, sql.host, sql.database)
        user_id = new_conn.get_user_id_by_chat(chat_id=chat_id)
        car_id = new_conn.get_car_id(user_id=user_id)
        dates_dict = new_conn.get_car_notification(car_id)
        current_date = datetime.datetime.now().date()
        # to notify the same day
        if current_date in [value
                            for value in dates_dict.values()]:
            return {'date': current_date,
                    'chat_id': chat_id,
                    'type': [key
                             for key in dates_dict
                             if dates_dict[key] == current_date]}

        else:
            return dict()

    except Exception:
        return False
def get_city_by_chat(chat):
    try:
        new_conn = sql.DataBase(sql.user, sql.password, sql.host, sql.database)
        return new_conn.get_user_city_by_chat(chat)

    except Exception:
        return False
def user_city(username):
    """

    :param username: str
    :return: str
    """

    new_conn = sql.DataBase(sql.user, sql.password, sql.host, sql.database)
    city = new_conn.get_user_city(username=username)

    return city
def user_in_database(user):
    """

    :param user: str
    :return: bool
    """

    new_conn = sql.DataBase(sql.user, sql.password, sql.host, sql.database)
    query_result = new_conn.select(table='users', columns=['name'])
    query_result_names = [item[0] for item in query_result]

    if user in query_result_names:
        return True

    return False
def add_year(message_chat):
    """

    :param message_chat: dict
    :return: bool
    """
    try:
        year = message_chat.text
        user = message_chat.chat.username
        new_conn = sql.DataBase(sql.user, sql.password, sql.host, sql.database)
        user_id = new_conn.get_user_id(user)
        new_conn.update_car_year(user_id, param_value=year)

        return True, None

    except Exception:
        return False, local.error_cannot_process + '\n' + local.try_again
def add_city(message_chat):
    """

    :param message_chat: dict
    :return: str
    """

    try:
        city = message_chat.text
        user = message_chat.chat.username
        new_conn = sql.DataBase(sql.user, sql.password, sql.host, sql.database)
        new_conn.update_user_city(username=user, param_value=city)

    except Exception:
        return False, local.error_cannot_process + '\n' + local.try_again

    return city, None
def add_tech(message_chat):
    """

        :param message_chat: dict
        :return: bool
    """
    try:
        tech = message_chat.text
        user = message_chat.chat.username
        new_conn = sql.DataBase(sql.user, sql.password, sql.host, sql.database)
        user_id = new_conn.get_user_id(user)
        car_id = new_conn.get_car_id(user_id)
        new_conn.tech_check_update(car_id, tech_check=tech)

        return True, None

    except Exception:
        return False, local.error_cannot_process + '\n' + local.datetime_format_warning + '\n' + local.try_again
def update_after_notification(message, keys):
    """

    :param message: str
    :param keys: list
    :return: bool or Exception
    """

    try:
        chat_id = message.chat.id
        for key in keys:
            new_conn = sql.DataBase(sql.user, sql.password, sql.host, sql.database)
            user_id = new_conn.get_user_id_by_chat(chat_id=chat_id)
            car_id = new_conn.get_car_id(user_id=user_id)
            today = datetime.datetime.now()
            getattr(new_conn, key+'_update')(car_id, today.strftime('%Y-%m-%d %H:%M:%S'))

        return True

    except Exception:
        return False
def add_password(message_chat):
    """

    :param message_chat: dict
    :return: str
    """

    try:
        password = message_chat.text
        if password_valid(password):
            new_conn = sql.DataBase(sql.user, sql.password, sql.host, sql.database)
            user = message_chat.chat.username
            new_conn.update_user_password(username=user, param_value=password)

        else:
            raise Exception(local.password_error)

    except Exception:
        return False, local.password_error + '\n' + local.try_again

    return password, None
def add_username(message_chat):
    """

    :param message_chat: dict
    :return: bool or Exception
    """

    try:
        name = message_chat.username
        chat_id = message_chat.id
        new_conn = sql.DataBase(sql.user, sql.password, sql.host, sql.database)
        new_conn.add_user(name=name, chat_id=chat_id)
        user_id = new_conn.get_user_id(username=name)
        new_conn.add_car(user_id=user_id)
        car_id = new_conn.get_car_id(user_id=user_id)
        new_conn.add_now(car_id=car_id)
        new_conn.add_future(car_id=car_id)

        return True, None

    except Exception:
        return False, local.error_cannot_process + '\n' + local.try_again