# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE

import jingrow


def get_notification_config():
	return {
		"for_pagetype": {
			"Error Log": {"seen": 0},
			"Email": {"status": "Open"},
			"Communication": {"status": "Open", "communication_type": "Communication"},
			"ToDo": "jingrow.core.notifications.get_things_todo",
			"Event": "jingrow.core.notifications.get_todays_events",
			"Workflow Action": {"status": "Open"},
		},
	}


def get_things_todo(as_list=False):
	"""Returns a count of incomplete todos"""
	data = jingrow.get_list(
		"ToDo",
		fields=["name", "description"] if as_list else "count(*)",
		filters=[["ToDo", "status", "=", "Open"]],
		or_filters=[
			["ToDo", "allocated_to", "=", jingrow.session.user],
			["ToDo", "assigned_by", "=", jingrow.session.user],
		],
		as_list=True,
	)

	if as_list:
		return data
	return data[0][0]


@jingrow.whitelist()
def get_todo_list(limit=20):
	"""Returns todo list for current user (for notification dropdown)"""
	return jingrow.get_list(
		"ToDo",
		fields=["name", "title", "description", "status", "date", "priority",
				"reference_type", "reference_name", "allocated_to", "assigned_by", "creation", "modified"],
		filters=[
			["ToDo", "status", "in", ["Todo", "In Progress"]],
			["ToDo", "allocated_to", "=", jingrow.session.user],
		],
		order_by="modified desc",
		limit_page_length=limit,
	)


def get_todays_events(as_list: bool = False):
	"""Returns a count of todays events in calendar"""
	from jingrow.desk.pagetype.event.event import get_events
	from jingrow.utils import nowdate

	today = nowdate()
	events = get_events(today, today)
	return events if as_list else len(events)
