# -*- coding: utf-8 -*-
"""
Created on Fri Feb 15 14:38:28 2019

@author: cpace
"""
from datetime import datetime

class contractsymbol:
    def __init__(self, crop, month, curdate):
        self.crop = crop.lower()
        self.month = month.lower()
        self.curdate = datetime.strptime(curdate, '%Y-%m-%d')
        self.monthcode = self.getmonthcode()
        self.cropcode = self.getcropcode()
        self.year = self.getyear()
        self.symbol = 'Z' + self.cropcode + self.monthcode + str(self.year)
        self.deliveryyear = self.getdeliveryyear()
        
    def getmonthcode(self):
        monthcode = {'corn':{'jan':'H', 'feb':'H', 'mar':'H', 'apr':'K', 'may':'K', 'jun':'N', 'jul':'N', 'aug':'U', 'sep':'Z', 'oct':'Z', 'nov':'Z', 'dec':'H'},
                     'soybeans':{'jan':'H', 'feb':'H', 'mar':'H', 'apr':'K', 'may':'K', 'jun':'N', 'jul':'Q', 'aug':'X', 'sep':'X', 'oct':'X', 'nov':'F', 'dec':'F'},
                     'wheat':{'jan':'H', 'feb':'H', 'mar':'H', 'apr':'K', 'may':'K', 'jun':'N', 'jul':'N', 'aug':'U', 'sep':'Z', 'oct':'Z', 'nov':'Z', 'dec':'H'}
                     }
        return monthcode[self.crop][self.month]
    
    def getmonthcodenumber(self):
        monthcode = str(self.getmonthcode())
        monthcodes = ['A', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'Q', 'U', 'W', 'X', 'Z']
        return monthcodes.index(monthcode)
        return True
    
    
    def getcropcode(self):
        cropcode = {'corn':'C', 'soybeans':'S', 'wheat':'W', 'natural gas':'NG', 'gasoline':'RB', 'crude oil':'CL'}
        return cropcode[self.crop]
    
    def getyear(self):
        if int(datetime.strftime(self.curdate, '%m')) < self.getmonthcodenumber():
            return int(datetime.strftime(self.curdate, '%Y'))
        else:
            return int(datetime.strftime(self.curdate, '%Y')) + 1

    def getdeliveryyear(self):
        if int(datetime.strftime(self.curdate, '%m')) <= int(datetime.strptime(self.month,'%b').strftime('%m')):
            return int(datetime.strftime(self.curdate, '%Y'))
        else:
            return int(datetime.strftime(self.curdate, '%Y')) + 1
        
