# Author:           Alexander M. Terp
# Date created:     January, 2016
# Description:      Contains code responsible for the functions used in the
#                   calculations for SVA.

import csv
from math import ceil

# Used to help user in case they enter information incorrectly.
from window import status

# Define global macro values.
SINGLE_HARVEST_CROPS_CSV_FILE = "csv_files/single_harvest_crops.csv"
REGENERATIVE_CROPS_CSV_FILE = "csv_files/regenerative_crops.csv"

DAYS_IN_SEASON = 28

DEBUG = 0

class Crop():
    """
    Defines a class for a general crop. Contains properties shared by all
    crops, whether they are a one-time harvest or multiple-time harvest However,
    contains all properties of a single-harvest crop.
    
    - name:             Name for the crop as referred to in-game.
    - buy_price:        The cheapest cost of an individual seed.
    - sell_price:       The regular sales price of one unit of produce.
    - growth_time:        The number of days it takes for the crop to 
                        become harvestable.
    - harvest_yield:    The average amount of produce per harvest.
    """
    def __init__(self, name, buy_price, sell_price, growth_time, harvest_yield):
        
        self.name = name
        self.buy_price = buy_price
        self.sell_price = sell_price
        self.growth_time = growth_time
        self.sell_price = sell_price
        self.harvest_yield = harvest_yield

        # Regenerative crops are defined using the RegenerativeCrop class, 
        # which will set this property as True instead.
        self.regenerative = False

        # Initialize net gold income per day (ngid) property. Will be changed
        # with future functions.
        self.ngid = None

class RegenerativeCrop(Crop):
    """
    Defines a class for a regenerative crop, such as a strawberry crop.
    - regrowth_time:        The number of days it takes for a mature crop to 
                            regrow and become harvestable again after being 
                            harvested.
    - max_harvests:    The number of times a crop can be harvested in a
                            season.
    """
    def __init__(self, name, buy_price, sell_price, growth_time, 
        harvest_yield, regrowth_time, max_harvests):
        Crop.__init__(self, name, buy_price, sell_price, growth_time, 
            harvest_yield)

        self.regrowth_time = regrowth_time
        self.max_harvests = max_harvests
        self.regenerative = True

class data:
    """ Define an object to hold widely used information. Will allow for easy
    argument-passing for functions. """
    crops = {}

def import_crops(data):
    """ Reads in data from csv files and populates the data object with crop
    objects. """
    with open(SINGLE_HARVEST_CROPS_CSV_FILE) as crop_values:
        crop_values = csv.DictReader(crop_values)
        for row in crop_values:
            info = [row["name"], int(row["buy_price"]), int(row["sell_price"]), 
                    int(row["growth_time"]), float(row["harvest_yield"])]
            crop = Crop(*info)
            crop.seasons = ([row["season1"], row["season2"]] if 
                            row["season2"] else [row["season1"]])

            data.crops[row["name"]] = crop