#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2011 Sebastian Pipping <sebastian@pipping.org>
# Licensed under GPL v3 or later

import freitagslib.network as net
from freitagslib.network import ITEM_ONLY, DEPOSIT_ONLY, ITEM_AND_DEPOSIT


class DepositCommand(object):
	def __init__(self, amount):
		self._difference = amount

	def difference(self):
		return self._difference

	def add(self, deposit_command):
		assert(isinstance(deposit_command, DepositCommand))
		self._difference += deposit_command._difference

	def run(self, user_name):
		net.deposit(self._difference, net.DEPOSIT_CASH, user_name)

	def label(self):
		return '%.2f Euro einzahlen' % self._difference


class BuyCommand(object):
	def __init__(self, item):
		self._item = item
		self._commodity = True
		self._deposit = False

	def _what_to_buy(self):
		if self._deposit and self._commodity:
			return ITEM_AND_DEPOSIT
		elif self._deposit:
			return DEPOSIT_ONLY
		elif self._commodity:
			return ITEM_ONLY
		else:
			raise ValueError("BuyCommand in illegal state")

	def difference(self):
		if self._deposit and self._commodity:
			return -(self._item.price + self._item.deposit)
		elif self._deposit:
			return -(self._item.deposit)
		elif self._commodity:
			return -(self._item.price)
		else:
			raise ValueError("BuyCommand in illegal state")

	def run(self, user_name):
		raise NotImplementedError('BuyCommand.run(user_name)')

	def item_name(self):
		return self._item.name

	def item_id(self):
		return self._item.id

	def commodity_label(self):
		if self._deposit or self.deposit_value() <= 0:
			return self._item.name
		else:
			return "%s (exkl. Pfand)" % self._item.name

	def deposit_label(self):
		return '%s Pfand' % self._item.name

	def includes_commodity(self):
		return self._commodity

	def includes_deposit(self):
		return self._deposit

	def include_commodity(self, commodity):
		self._commodity = commodity

	def include_deposit(self, deposit):
		self._deposit = deposit

	def commodity_value(self):
		return self._item.price

	def deposit_value(self):
		return self._item.deposit
