#   pyRCV2: Preferential vote counting
#   Copyright © 2020  Lee Yingtong Li (RunasSudo)
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.

import functools
import math

def compatible_types(f):
	@functools.wraps(f)
	def wrapper(self, other):
		if not isinstance(other, Native):
			raise ValueError('Attempt to operate on incompatible types')
		return f(self, other)
	return wrapper

class Native:
	"""
	Wrapper for Python float (naive floating-point arithmetic)
	"""
	
	ROUND_DOWN = 0
	ROUND_HALF_UP = 1
	ROUND_HALF_EVEN = 2
	ROUND_UP = 3
	
	def __init__(self, val):
		if isinstance(val, Native):
			self.impl = val.impl
		else:
			self.impl = float(val)
	
	def __repr__(self):
		return '<Native {}>'.format(str(self.impl))
	def pp(self, dp):
		"""Pretty print to specified number of decimal places"""
		return format(self.impl, '.{}f'.format(dp))
	
	def to_rational(self):
		"""Convert to an instance of Rational"""
		from pyRCV2.numbers import Rational
		return Rational(self.impl)
	
	@compatible_types
	def __add__(self, other):
		return Native(self.impl + other.impl)
	@compatible_types
	def __sub__(self, other):
		return Native(self.impl - other.impl)
	@compatible_types
	def __mul__(self, other):
		return Native(self.impl * other.impl)
	@compatible_types
	def __truediv__(self, other):
		return Native(self.impl / other.impl)
	
	@compatible_types
	def __eq__(self, other):
		return self.impl == other.impl
	@compatible_types
	def __gt__(self, other):
		return self.impl > other.impl
	@compatible_types
	def __ge__(self, other):
		return self.impl >= other.impl
	@compatible_types
	def __lt__(self, other):
		return self.impl < other.impl
	@compatible_types
	def __le__(self, other):
		return self.impl <= other.impl
	
	def __floor__(self):
		return Native(math.floor(self.impl))
	
	def round(self, dps, mode):
		"""Round to the specified number of decimal places, using the ROUND_* mode specified"""
		factor = 10 ** dps
		if mode == Native.ROUND_DOWN:
			return Native(math.floor(self.impl * factor) / factor)
		elif mode == Native.ROUND_HALF_UP:
			raise Exception('ROUND_HALF_UP is not implemented in Python Native context')
		elif mode == Native.ROUND_HALF_EVEN:
			return Native(round(self.impl * factor) / factor)
		elif mode == Native.ROUND_UP:
			return Native(math.ceil(self.impl * factor) / factor)
		else:
			raise Exception('Invalid rounding mode')
