# -*- coding: utf-8 -*-
# Copyright (c) 2019, Frappe Technologies and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
from frappe.model.document import Document
from frappe.modules.export_file import export_to_files
import frappe
from frappe import _
import json

class Dashboard(Document):
	def on_update(self):
		if self.is_default:
			# make all other dashboards non-default
			frappe.db.sql('''update
				tabDashboard set is_default = 0 where name != %s''', self.name)

		if frappe.conf.developer_mode and self.is_standard:
			export_to_files(record_list=[['Dashboard', self.name, self.module + ' Dashboard']], record_module=self.module)

	def validate(self):
		if not frappe.conf.developer_mode and self.is_standard:
			frappe.throw('Cannot edit Standard Dashboards')
		self.validate_custom_options()

	def validate_custom_options(self):
		if self.chart_options:
			try:
				json.loads(self.chart_options)
			except ValueError as error:
				frappe.throw(_("Invalid json added in the custom options: {0}").format(error))

@frappe.whitelist()
def get_permitted_charts(dashboard_name):
	permitted_charts = []
	dashboard = frappe.get_doc('Dashboard', dashboard_name)
	for chart in dashboard.charts:
		if frappe.has_permission('Dashboard Chart', doc=chart.chart):
			chart_dict = frappe._dict()
			chart_dict.update(chart.as_dict())

			if dashboard.get('chart_options'):
				chart_dict.custom_options = dashboard.get('chart_options')
			permitted_charts.append(chart_dict)

	return permitted_charts

@frappe.whitelist()
def get_permitted_cards(dashboard_name):
	permitted_cards = []
	dashboard = frappe.get_doc('Dashboard', dashboard_name)
	for card in dashboard.cards:
		if frappe.has_permission('Number Card', doc=card.card):
			permitted_cards.append(card)
	return permitted_cards

@@ -822,16 +822,438 @@
 oards')%0A
+%0A%09%09if self.is_standard:%0A%09%09%09non_standard_docs_map = %7B%0A%09%09%09%09'Dashboard Chart': get_non_standard_charts_in_dashboard(self),%0A%09%09%09%09'Number Card': get_non_standard_cards_in_dashboard(self)%0A%09%09%09%7D%0A%0A%09%09%09if non_standard_docs_map%5B'Dashboard Chart'%5D or non_standard_docs_map%5B'Number Card'%5D:%0A%09%09%09%09message = get_non_standard_warning_message(non_standard_docs_map)%0A%09%09%09%09frappe.throw(message, title=_(%22Standard Not Set%22), is_minimizable=True)%0A%0A
 %09%09self.v
@@ -2256,8 +2256,1075 @@
 d_cards%0A
+%0Adef get_non_standard_charts_in_dashboard(dashboard):%0A%09non_standard_charts = %5Bdoc.name for doc in frappe.get_list('Dashboard Chart', %7B'is_standard': 0%7D)%5D%0A%09return %5Bchart_link.chart for chart_link in dashboard.charts if chart_link.chart in non_standard_charts%5D%0A%0Adef get_non_standard_cards_in_dashboard(dashboard):%0A%09non_standard_cards = %5Bdoc.name for doc in frappe.get_list('Number Card', %7B'is_standard': 0%7D)%5D%0A%09return %5Bcard_link.card for card_link in dashboard.cards if card_link.card in non_standard_cards%5D%0A%0Adef get_non_standard_warning_message(non_standard_docs_map):%0A%09message = _('''Please set the following documents in this Dashboard as standard first.''')%0A%0A%09def get_html(docs, doctype):%0A%09%09html = '%3Cp%3E%7B%7D%3C/p%3E'.format(frappe.bold(doctype))%0A%09%09for doc in docs:%0A%09%09%09html += '%3Cdiv%3E%3Ca href=%22#Form/%7Bdoctype%7D/%7Bdoc%7D%22%3E%7Bdoc%7D%3C/a%3E%3C/div%3E'.format(doctype=doctype, doc=doc)%0A%09%09html += '%3Cbr%3E'%0A%09%09return html%0A%0A%09html = message + '%3Cbr%3E'%0A%0A%09for doctype in non_standard_docs_map:%0A%09%09if non_standard_docs_map%5Bdoctype%5D:%0A%09%09%09html += get_html(non_standard_docs_map%5Bdoctype%5D, doctype)%0A%0A%09return html%0A

validate if all docs in dashboard have been set as standard