# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE
import csv
import json
from io import StringIO

import requests

import jingrow
from jingrow import _, msgprint
from jingrow.utils import cint, comma_or, cstr, flt


def read_csv_content_from_attached_file(pg):
	fileid = jingrow.get_all(
		"File",
		fields=["name"],
		filters={"attached_to_pagetype": pg.pagetype, "attached_to_name": pg.name},
		order_by="creation desc",
	)

	if fileid:
		fileid = fileid[0].name

	if not fileid:
		msgprint(_("File not attached"))
		raise Exception

	try:
		_file = jingrow.get_pg("File", fileid)
		fcontent = _file.get_content()
		return read_csv_content(fcontent)
	except Exception:
		jingrow.throw(
			_("Unable to open attached file. Did you export it as CSV?"), title=_("Invalid CSV Format")
		)


def read_csv_content(fcontent):
	if not isinstance(fcontent, str):
		decoded = False
		for encoding in ["utf-8", "windows-1250", "windows-1252"]:
			try:
				fcontent = str(fcontent, encoding)
				decoded = True
				break
			except UnicodeDecodeError:
				continue

		if not decoded:
			jingrow.msgprint(
				_("Unknown file encoding. Tried utf-8, windows-1250, windows-1252."), raise_exception=True
			)

	fcontent = fcontent.encode("utf-8")
	content = [jingrow.safe_decode(line) for line in fcontent.splitlines(True)]

	try:
		rows = []
		for row in csv.reader(content):
			r = []
			for val in row:
				# decode everything
				val = val.strip()

				if val == "":
					# reason: in maraidb strict config, one cannot have blank strings for non string datatypes
					r.append(None)
				else:
					r.append(val)

			rows.append(r)

		return rows

	except Exception:
		jingrow.msgprint(_("Not a valid Comma Separated Value (CSV File)"))
		raise


@jingrow.whitelist()
def send_csv_to_client(args):
	if isinstance(args, str):
		args = json.loads(args)

	args = jingrow._dict(args)

	jingrow.response["result"] = cstr(to_csv(args.data))
	jingrow.response["pagetype"] = args.filename
	jingrow.response["type"] = "csv"


def to_csv(data):
	writer = UnicodeWriter()
	for row in data:
		writer.writerow(row)

	return writer.getvalue()


def build_csv_response(data, filename):
	jingrow.response["result"] = cstr(to_csv(data))
	jingrow.response["pagetype"] = filename
	jingrow.response["type"] = "csv"


class UnicodeWriter:
	def __init__(self, encoding="utf-8", quoting=csv.QUOTE_NONNUMERIC):
		self.encoding = encoding
		self.queue = StringIO()
		self.writer = csv.writer(self.queue, quoting=quoting)

	def writerow(self, row):
		self.writer.writerow(row)

	def getvalue(self):
		return self.queue.getvalue()


def check_record(d):
	"""check for mandatory, select options, dates. these should ideally be in doclist"""
	from jingrow.utils.dateutils import parse_date

	pg = jingrow.get_pg(d)

	for key in d:
		pagefield = pg.meta.get_field(key)
		val = d[key]
		if pagefield:
			if pagefield.reqd and (val == "" or val is None):
				jingrow.msgprint(_("{0} is required").format(pagefield.label), raise_exception=1)

			if pagefield.fieldtype == "Select" and val and pagefield.options:
				if val not in pagefield.options.split("\n"):
					jingrow.throw(
						_("{0} must be one of {1}").format(
							_(pagefield.label, context=pagefield.parent), comma_or(pagefield.options.split("\n"))
						)
					)

			if val and pagefield.fieldtype == "Date":
				d[key] = parse_date(val)
			elif val and pagefield.fieldtype in ["Int", "Check"]:
				d[key] = cint(val)
			elif val and pagefield.fieldtype in ["Currency", "Float", "Percent"]:
				d[key] = flt(val)


def import_pg(d, pagetype, overwrite, row_idx, submit=False, ignore_links=False):
	"""import main (non child) document"""
	if d.get("name") and jingrow.db.exists(pagetype, d["name"]):
		if overwrite:
			pg = jingrow.get_pg(pagetype, d["name"])
			pg.flags.ignore_links = ignore_links
			pg.update(d)
			if d.get("pagestatus") == 1:
				pg.update_after_submit()
			elif d.get("pagestatus") == 0 and submit:
				pg.submit()
			else:
				pg.save()
			return "Updated row (#%d) %s" % (row_idx + 1, getlink(pagetype, d["name"]))
		else:
			return "Ignored row (#%d) %s (exists)" % (row_idx + 1, getlink(pagetype, d["name"]))
	else:
		pg = jingrow.get_pg(d)
		pg.flags.ignore_links = ignore_links
		pg.insert()

		if submit:
			pg.submit()

		return "Inserted row (#%d) %s" % (row_idx + 1, getlink(pagetype, pg.get("name")))


def getlink(pagetype, name):
	return '<a href="/app/Form/{pagetype}/{name}">{name}</a>'.format(**locals())


def get_csv_content_from_google_sheets(url):
	# https://pgs.google.com/spreadsheets/d/{sheetid}}/edit#gid={gid}
	validate_google_sheets_url(url)
	# get gid, defaults to first sheet
	if "gid=" in url:
		gid = url.rsplit("gid=", 1)[1]
	else:
		gid = 0
	# remove /edit path
	url = url.rsplit("/edit", 1)[0]
	# add /export path,
	url = url + f"/export?format=csv&gid={gid}"

	headers = {"Accept": "text/csv"}
	response = requests.get(url, headers=headers)

	if response.ok:
		# if it returns html, it couldn't find the CSV content
		# because of invalid url or no access
		if response.text.strip().endswith("</html>"):
			jingrow.throw(
				_("Google Sheets URL is invalid or not publicly accessible."), title=_("Invalid URL")
			)
		return response.content
	elif response.status_code == 400:
		jingrow.throw(
			_(
				'Google Sheets URL must end with "gid={number}". Copy and paste the URL from the browser address bar and try again.'
			),
			title=_("Incorrect URL"),
		)
	else:
		response.raise_for_status()


def validate_google_sheets_url(url):
	from urllib.parse import urlparse

	u = urlparse(url)
	if u.scheme != "https" or u.netloc != "pgs.google.com" or "/spreadsheets/" not in u.path:
		jingrow.throw(
			_('"{0}" is not a valid Google Sheets URL').format(url),
			title=_("Invalid URL"),
		)
