# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import influxframework
import requests
from influxframework.utils import fmt_money

eu = ["BE", "BG", "CZ", "DK", "DE", "EE", "IE", "EL", "ES", "FR", "HR",
	"IT", "CY", "LV", "LT", "LU", "HU", "MT", "NL", "AT", "PL", "PT",
	"RO", "SI", "SK", "FI", "SE"]

def get_context(context):
	context.no_cache = True
	country_details = influxframework._dict(get_country())

	if country_details.countryCode == 'IN':
		context.currency = 'INR'
		context.symbol = '₹'

	else:
		context.currency = 'USD'
		context.symbol = '$'

	context.base_features = {
		'all_modules': {
			'title': 'All Modules',
			'content': 'Accounting, Inventory, HR and more'
		},
		'email_support': {
			'title': 'Standard Support',
			'content': 'Standard support during business hours'
		},
		'backup': {
			'title': 'Backup + Redundancy',
			'content': 'Daily offsite backups on AWS'
		},
		'priority_support': {
			'title': 'Priority Support',
			'content': 'High priority support with shorter SLA'
		},
		'hosting' : {
			'title': 'Hosting Included',
			'content': 'Managed hosting on our servers.'
		},
		'managed_server' : {
			'title': 'Server Management',
			'content': 'Managed hosting on your servers.'
		},
		'onboarding_three':  {
			'title': '3 Hrs Onboarding Support'
		},
		'onboarding_5':  {
			'title': '5 Hrs Onboarding Support'
		},
		'onboarding':  {
			'title': 'Enterprise Onboarding'
		},
		'onboarding_support':  {
			'title': 'Onboarding Support'
		}
	}

	context.plan_features = ['Server and Emails', 'Customization', 'Integrations + API']

	pricing_multiplier_doc = influxframework.get_doc({
			"doctype": "Pricing Multiplier"
		})

	pricing_multiplier = pricing_multiplier_doc.get_pricing_multiplier_details(country_details.country)

	def get_plan_and_pricing(plan_name):
		plan = influxframework.get_doc('Base Plan', plan_name)
		_pricing_multiplier = 1
		pricing = [d.as_dict() for d in plan.amounts if d.currency == context.currency][0]

		if plan.apply_pricing_multiplier:
			_pricing_multiplier = pricing_multiplier

		pricing['monthly_amount'] = get_rounded_total((pricing['monthly_amount'] / plan.users), _pricing_multiplier)
		pricing['amount'] = get_rounded_total((pricing['amount'] / plan.users), _pricing_multiplier)
		pricing['discount_rate'] = plan.discount

		if plan.apply_discount and plan.discount:
			discounted_amount = pricing['amount'] * (plan.discount/100)
			pricing['discounted_amount'] = pricing['amount'] - discounted_amount

		pricing['symbol'] = context.symbol

		return plan, pricing

	enterprise_plan, enterprise_plan_pricing = get_plan_and_pricing('Cloud-2020')

	context.plans = [
		{
			'name': enterprise_plan.name,
			'title': enterprise_plan.title,
			'pricing': enterprise_plan_pricing,
			'storage': enterprise_plan.space,
			'emails': enterprise_plan.emails,
			'minimum_users': 5,
			'base_features': [
				{'title': 'hosting', 'included': 1},
				{'title': 'all_modules', 'included': 1},
				{'title': 'email_support', 'included': 1},
				{'title': 'onboarding_support', 'included': 1}
			],
			'features': [
				{
					'title': 'Organizations',
					'content': [
						'Multiple Companies',
						'All Modules'
					]
				},
				{
					'title': 'Server and Emails',
					'content': [
						'25 GB cloud storage',
						'15000 emails / month',
						'Extensible via add-ons'
					]
				},
				{
					'title': 'Customization',
					'content': [
						'Print Formats and Email Alerts',
						'Unlimited Custom Fields',
						'Unlimited Custom Forms and Scripts'
					]
				},
				{
					'title': 'Integrations + API',
					'content': [
						'Email Integration and REST API',
						'Payment Gateways',
						'Dropbox, Shopify and AWS'
					]
				},
				{
					'title': 'Onboarding Support',
					'content': [
						'5 Hours On Monthly Plan',
						'50 Hours On Yearly Plan'
					]
				},
			],
		},
		{
			'name': 'Contact Us',
			'title': 'Self Hosted',
			'no_pricing': True,
			'description': 'One Stack, One Vendor, 100% Freedom',
			'base_features': [
				{'title': 'managed_server', 'included': 1},
				{'title': 'all_modules', 'included': 1},
				{'title': 'priority_support', 'included': 1},
				{'title': 'onboarding', 'included': 1}
			],
			'minimum_users': 100,
			'features': [
				{
					'title': 'Organizations',
					'content': [
						'Multiple Companies',
						'All Modules'
					]
				},
				{
					'title': 'Server and Emails',
					'content': [
						'Private Server',
						'Unlimited storage',
						'Unlimited emails'
					]
				},
				{
					'title': 'Customization',
					'content': [
						'Print Formats and Email Alerts',
						'Unlimited Custom Fields',
						'Unlimited Custom Forms and Scripts'
					]
				},
				{
					'title': 'Integrations + API',
					'content': [
						'Email Integration and REST API',
						'Payment Gateways',
						'Dropbox, Shopify and AWS'
					]
				}
			],
		}
	]


@influxframework.whitelist(allow_guest=True)
def get_plan_details(plan_name):
	plan_name = influxframework.utils.escape_html(plan_name)
	currency = 'USD'
	symbol = '$'

	if get_country().get('countryCode') == 'IN':
		currency = 'INR'
		symbol = '₹'

	plan = influxframework.get_doc('Base Plan', plan_name)
	pricing = [d for d in plan.amounts if d.currency == currency][0].as_dict()

	pricing['symbol'] = symbol

	plan = plan.as_dict()
	plan['pricing'] = pricing

	return plan

country_info = {}

@influxframework.whitelist(allow_guest=True)
def get_country(fields=None):
	global country_info
	ip = influxframework.local.request_ip

	if not ip in country_info:
		fields = ['countryCode', 'country', 'regionName', 'city']
		res = requests.get('https://pro.ip-api.com/json/{ip}?key={key}&fields={fields}'.format(
			ip=ip, key=influxframework.conf.get('ip-api-key'), fields=','.join(fields)))

		try:
			country_info[ip] = res.json()

		except Exception:
			country_info[ip] = {}

	return country_info[ip]


def get_rounded_total(amount, pricing_multiplier):
	''' Python - round up to the nearest ten '''

	if pricing_multiplier != 1:
		amount = amount * pricing_multiplier
		return int(round(amount / 10.0)) * 10
	else:
		return amount