import datetime
import pandas as pd
from xlsxwriter.workbook import Workbook
from paperpusher import spreadsheet
	
class SummaryVariable():
	"""Directions for how to summarize a given varaible
		
		Attributes:
			name: The summary variable's name
			variables: a list of variables
			methods: [list] A list of string method names
			groups : [list] a of Group objects applied to this summary variable
			
			method_spreadsheet_columns: [dictionary] dictionary in the form 
				
				{"method_name" : column number}

				where the column number correspond to the column of the method name in the spreadsheet
				
			subheader_row_num : [int] subheader row number in the worksheet
			last_group_row_num : [int] row number of the last group in the worksheet

	"""
	
	def __init__(self, name, variables = [], methods = []):
		self.name = name
		self.variables = variables # a list of variables
		self.methods = methods
		self.method_spreadsheet_columns = {}
		self.groups = []
		
	def apply_method(self, data_frame, method_name, group):
		"""Applied the method specified by string to this variable
			
			Args:
				data_frame: A pandas dataframe object
				method_name: string representing one of the SummaryVariable analytic methods
				group : [Group] a group to subset the data frame by
				
			Returns:
				Returns the return value of a specified method. If no method matches
				the string, returns null.
		"""
		
		# total number of observations before subsetting by applying group condition
		total_num_observations = len(data_frame.index)
		# apply groups
		data_frame = self.apply_group(data_frame, group)
		
		if method_name == "min":
			return self.min(data_frame)
		elif method_name == "max":
			return self.max(data_frame)
		elif method_name == "mean":
			return self.mean(data_frame)
		elif method_name == "median":
			return self.median(data_frame)
		elif method_name == "sum":
			return self.sum(data_frame)
		elif method_name == "percent_of_sum":
			return self.percent_of_sum(data_frame)