from .database import *


def get_backdrop_schema(name: str) -> dict[str, dict[str, Any]]:
    return { 'content' : {'type': 'str' , 'optional': False , 'readonly': False} }


def get_backdrop(name: str) -> dict[str, Any]:
    e = read(name, f"select * from backdrop")
    return { 'content': e['content'] }


def _set_backdrop(name: str, cs: ChangeSet) -> DbChangeSet:
    old = get_backdrop(name)

    redo_sql = f"update backdrop set content = '{cs.operations[0]['content']}' where content = '{old['content']}';"
    undo_sql = f"update backdrop set content = '{old['content']}' where content = '{cs.operations[0]['content']}';"

    redo_cs = g_update_prefix | { 'type': 'backdrop', 'content': cs.operations[0]['content'] }
    undo_cs = g_update_prefix | { 'type': 'backdrop', 'content': old['content'] }

    return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])


def set_backdrop(name: str, cs: ChangeSet) -> ChangeSet:
    return execute_command(name, _set_backdrop(name, cs))


def inp_in_backdrop(section: list[str]) -> str:
    if section == []:
        return str('')

    content = '\n'.join(section)
    return str(f"update backdrop set content = '{content}';")


def inp_out_backdrop(name: str) -> list[str]:
    obj = str(get_backdrop(name)['content'])
    return obj.split('\n')