from patch.include import *


@app_context(commit=True)
def events_to_list_1():
    events = models.CmfEvent.list(
        fields=[
            "cmf_modified_by",
            "cmf_locked_by",
            "cmf_author",
            "cmf_owner",
            "object.status",
            "object.plan_end_date",
            "object.plan_start_date",
            "object.time_estimate",
        ],
    )
    persons = models.CmfPerson.list(["vacation_interval"])

    def get_person_by_login(login):
        for person in persons:
            try:
                if person.login == login:
                    return person
            except ValueError:
                pass

    for event in events:
        if "Смержить devel в master" in event.msg_subject:
            print(event)
        event_ui_targets = event.event_ui_targets
        if not event_ui_targets:
            event_ui_targets = ["doing"]
        for event_ui_target in event_ui_targets:
            card = models.CmfCard()
            card.cmf_author = event.cmf_author
            card.cmf_author_id = event.cmf_author_id
            card.cmf_created_at = event.cmf_created_at
            card.cmf_deleted = event.cmf_deleted
            card.cmf_locked_at = event.cmf_locked_at
            card.cmf_locked_by = event.cmf_locked_by
            card.cmf_locked_by_id = event.cmf_locked_by_id
            card.cmf_modified_at = event.cmf_modified_at
            card.cmf_modified_by = event.cmf_modified_by
            card.cmf_modified_by_id = event.cmf_modified_by_id
            card.cmf_owner = event.cmf_owner
            card.cmf_owner_id = event.cmf_owner_id
            card.cmf_version = event.cmf_version
            card.cmf_viewed_at = event.cmf_viewed_at
            if event.code:
                card.code = event.code.value.replace(
                    event.code_prefix, card.code_prefix
                )
            else:
                card.code = event.code
            card.obj_alarm_date = event.event_alarm_date
            card.person_id = event.event_person_id
            # event.event_person_type
            card.status = event.event_status
            card.pinned = event.event_stick
            if event_ui_target == "notify":
                card.place = "notify"
            elif event_ui_target == "approve":
                card.place = "approve"
            elif event_ui_target == "doing":
                card.place = "doing"
            elif event_ui_target == "spectator":
                card.place = "spectate"
            elif event_ui_target == "owner":
                card.place = "assigned"
            else:
                assert False, event_ui_target
            # event.feeds
            card.id = card.gen_id()
            # event.msg_alarm_date
            card.text = event.msg_body
            card.obj_ui_name = event.object.ui_name

            owner = get_person_by_login(event.msg_from)

            if owner:
                card.obj_cmf_owner_id = owner.id
                card.obj_cmf_owner_name = owner.name
                card.obj_cmf_owner_code = owner.code
                card.obj_cmf_owner_login = owner.login

            responsible = get_person_by_login(event.msg_to)

            if responsible:
                card.obj_responsible_id = responsible.id
                card.obj_responsible_name = responsible.name
                card.obj_responsible_code = responsible.code
                card.obj_responsible_login = responsible.login

            # event.msg_icon
            card.obj_priority = event.msg_important
            card.orderno = event.msg_orderno
            card.obj_orderno = event.msg_orderno

            # msg_penalty
            card.obj_status = event.object.status
            card.obj_status_type = event.msg_status
            card.name = event.msg_subject
            # msg_to
            card.obj_url = event.msg_url
            # name
            card.obj_id = event.object_id
            card.obj_name = event.object.name
            card.obj_code = event.object_code
            # text
            card.unread_cnt = event.unread_cnt
            # view_object
            # view_object_code
            # view_object_id
            card.obj_plan_end_date = event.object.plan_end_date
            card.obj_plan_start_date = event.object.plan_start_date
            card.obj_time_estimate = event.object.time_estimate
            card.save()


@app_context(commit=True)
def events_to_list_2():
    places = {
        "notify": "notify",
        "approve": "approve",
        "doing": "doing",
        # "in_work": "doing",
        "spectator": "spectate",
        "owner": "assigned",
    }
    for load_card_notify, place in places.items():

        # old_cards = models.CmfEvent.card_list(load_card_notify=load_card_notify, filter_preset=filter_preset)
        old_cards = models.CmfEvent.card_list(load_card_notify=load_card_notify)
        for old_card in old_cards:
            card = models.CmfCard()

            card.cmf_author = old_card.cmf_author
            card.cmf_author_id = old_card.cmf_author_id
            card.cmf_created_at = old_card.cmf_created_at
            card.cmf_deleted = old_card.cmf_deleted
            card.cmf_locked_at = old_card.cmf_locked_at
            card.cmf_locked_by = old_card.cmf_locked_by
            card.cmf_locked_by_id = old_card.cmf_locked_by_id
            card.cmf_modified_at = old_card.cmf_modified_at
            card.cmf_modified_by = old_card.cmf_modified_by
            card.cmf_modified_by_id = old_card.cmf_modified_by_id
            card.cmf_owner = old_card.cmf_owner
            card.cmf_owner_id = old_card.cmf_owner_id
            card.cmf_version = old_card.cmf_version
            card.cmf_viewed_at = old_card.cmf_viewed_at

            card.name = old_card.name
            card.text = old_card["notify"].value["msg_body"]
            card.place = place
            if place == "notify":
                card.pinned = False
            else:
                card.pinned = not old_card["notify"].value["notify_cards"]
            card.status = (
                old_card.status
                and old_card.status.status_type == "closed"
                and "closed"
                or "open"
            )
            card.orderno = old_card.orderno
            card.person_id = old_card.waiting_for.id
            card.obj_id = old_card.id
            card.obj_code = old_card.code
            card.obj_name = old_card.name
            card.obj_ui_name = old_card.ui_name
            card.icon_svg = None
            card.obj_url = None
            card.obj_cmf_owner_id = old_card.cmf_owner.id
            card.obj_cmf_owner_name = old_card.cmf_owner.name
            card.obj_cmf_owner_login = old_card.cmf_owner.login
            if old_card.responsible:
                card.obj_responsible_id = old_card.responsible.id
                card.obj_responsible_name = old_card.responsible.name
                card.obj_responsible_login = old_card.responsible.login
            card.obj_priority = old_card.priority
            card.obj_orderno = old_card.orderno
            card.obj_alarm_date = old_card.alarm_date
            card.obj_deadline = old_card.deadline
            card.obj_time_estimate = old_card.time_estimate
            card.obj_plan_start_date = old_card.plan_start_date
            card.obj_plan_end_date = old_card.plan_end_date
            card.obj_status = old_card.status
            if old_card.status:
                card.obj_status_type = old_card.status.status_type

            card.unread_cnt = old_card["notify"].value["unread_cnt"]

            card.save()


@app_context(commit=True)
def events_to_list_3():
    tasks = models.CmfTask.list(
        fields=[
            "id",
            "waiting_for",
            "cache_status_type",
            "orderno",
            "cmf_owner",
            "cmf_author",
            "responsible",
            "priority",
            "alarm_date",
            "deadline",
            "time_estimate",
            "plan_start_date",
            "plan_end_date",
            "status",
            "cmf_created_at",
            "cmf_modified_at",
        ],
        filter=[
            ["alarm_date", "!=", None],
            ["cache_status_type", "!=", "closed"],
            # ["waiting_for_id", "==", "CmfPerson:bbd21874-9072-11ea-b5fe-fd64afb23b28"],
            # ["alarm_date", "<", "2021-01-14T14:23:11.822069Z"],
        ],
    )

    for task in tasks:
        last_event = models.CmfEvent.get(
            filter=[
                ["object_id", "==", task.id],
                ["event_person_id", "==", task.waiting_for.id],
            ],
            order_by=["-cmf_modified_at"],
        )

        card = models.CmfCard()
        card.text = last_event and last_event.msg_body or ""
        if task.cache_status_type == "in_review":
            card.place = "approve"
            card.name = "Утвердите"
        else:
            card.place = "doing"
            card.name = "Выполните"
        card.pinned = True
        card.orderno = task.orderno
        card.person_id = task.waiting_for.id
        card.status = "open"

        card.obj_id = task.id
        card.obj_code = task.code
        card.obj_name = task.name or ""
        card.obj_url = None  # TODO: TODO0: task.url

        if task.cmf_owner:
            card.obj_cmf_owner_id = task.cmf_owner.id
            card.obj_cmf_owner_name = task.cmf_owner.name
            card.obj_cmf_owner_login = task.cmf_owner.login

        if task.responsible and "@" in task.responsible.login.value:
            card.obj_responsible_id = task.responsible.id
            card.obj_responsible_name = task.responsible.name
            card.obj_responsible_login = task.responsible.login

        card.obj_priority = task.priority

        card.obj_orderno = task.orderno
        if task.orderno.is_changed:
            card.orderno = task.orderno

        card.obj_alarm_date = task.alarm_date
        card.obj_deadline = task.deadline
        card.obj_time_estimate = task.time_estimate
        card.obj_plan_start_date = task.plan_start_date
        card.obj_plan_end_date = task.plan_end_date
        card.obj_status = task.status
        card.obj_status_type = task.status.status_type
        card.obj_ui_name = task.ui_name

        card.cmf_created_at = task.cmf_created_at
        card.cmf_modified_at = task.cmf_modified_at

        card.save()

        if (task.responsible and task.responsible != task.cmf_owner) or (
            task.waiting_for and task.waiting_for != task.cmf_owner
        ):
            card.is_new = True
            card.id = card.gen_id()
            card.code = card.gen_code()
            card.person_id = task.cmf_owner.id
            card.place = "assigned"
            card.save()

        for spectator in task.spectators.all_nested():
            card.is_new = True
            card.id = card.gen_id()
            card.code = card.gen_code()
            card.person_id = spectator.id
            card.place = "spectate"
            card.save()

        for executor in task.executors.all_nested():
            card.is_new = True
            card.id = card.gen_id()
            card.code = card.gen_code()
            card.person_id = executor.id
            card.place = "doing"
            card.save()

    events = models.CmfEvent.list(
        fields=[
            "object_id",
            "object_waiting_for",
            "object_cache_status_type",
            "object_orderno",
            "object_cmf_owner",
            "object_cmf_author",
            "object_responsible",
            "object_priority",
            "object_alarm_date",
            "object_deadline",
            "object_time_estimate",
            "object_plan_start_date",
            "object_plan_end_date",
            "object_status",
            "object_cmf_created_at",
            "object_cmf_modified_at",
        ],
        filter=[
            ["event_ui_targets", "LIKE", "%notify%"],
            ["event_status", "==", "open"],
        ],
    )
    for last_event in events:
        task = last_event.object

        card = models.CmfCard()
        card.name = "Уведомление"
        card.text = last_event and last_event.msg_body or ""
        card.place = "notify"
        card.pinned = False
        card.orderno = task.orderno
        card.person_id = last_event.event_person.id
        card.status = "open"

        card.obj_id = task.id
        card.obj_code = task.code
        card.obj_name = task.name or ""
        card.obj_url = None  # TODO: TODO0: task.url

        if task.cmf_owner:
            card.obj_cmf_owner_id = task.cmf_owner.id
            card.obj_cmf_owner_name = task.cmf_owner.name
            card.obj_cmf_owner_login = task.cmf_owner.login

        if (
            task.responsible
            and task.responsible.login
            and "@" in task.responsible.login.value
        ):
            card.obj_responsible_id = task.responsible.id
            card.obj_responsible_name = task.responsible.name
            card.obj_responsible_login = task.responsible.login

        card.obj_priority = task.priority

        card.obj_orderno = task.orderno
        if task.orderno.is_changed:
            card.orderno = task.orderno

        card.obj_alarm_date = task.alarm_date
        card.obj_deadline = task.deadline
        card.obj_time_estimate = task.time_estimate
        card.obj_plan_start_date = task.plan_start_date
        card.obj_plan_end_date = task.plan_end_date
        card.obj_status = task.status
        card.obj_status_type = task.status.status_type
        card.obj_ui_name = task.ui_name

        card.cmf_created_at = last_event.cmf_created_at
        card.cmf_modified_at = last_event.cmf_modified_at

        card.save()

    print(len(events))


if __name__ == "__main__":
    events_to_list_3()
