import pandas as pd
import datetime
import numpy as np
import plotly as pl
import plotly.graph_objs as go
import matplotlib.pyplot as plt
import math
import scipy
import random
import statistics 


import datetime 
import matplotlib.dates as mdates
import matplotlib.pyplot as plt

import mplfinance as mpf

import plotly
#import plotly.plotly as py
import plotly.graph_objs as go
# these two lines allow your code to show up in a notebook
from plotly.offline import init_notebook_mode, iplot
from plotly.subplots import make_subplots
init_notebook_mode()

import market_trade.core.CoreTraidMath
import market_trade.core.CoreDraw
import market_trade.core.Ind_Alligator

class Gator:

    def __init__(self, base_df,options={}, needFig=False,showOnlyIndex=True,drawFig=False):
        self.base_df=base_df.reset_index(drop=True)
        self.options=options
        self.ans=self.getAns()
        if needFig:
            self.fig=self.pltShow(showOnlyIndex,drawFig)
            
            
    def getAns(self):
        
        req=Ind_Alligator.Alligator(self.base_df,self.options).ans
        #self.req=req
        '''
        ans={'Jaw-Teeth':{'y':[],'x':[]},
             'Teeth-Lips':{'y':[],'x':[]},
            }
        JawTeethIter=self.options['Jaw']['window']+self.options['Jaw']['shift']-self.options['Teeth']['window']-self.options['Teeth']['shift']
        TeethLipsIter=self.options['Teeth']['window']+self.options['Teeth']['shift']-self.options['Lips']['window']-self.options['Lips']['shift']
        
        #print(TeethLipsIter)
        for i in range(len(req['Jaw']['y'])):
            ans['Jaw-Teeth']['y'].append(abs(req['Jaw']['y'][i]-req['Teeth']['y'][JawTeethIter+i]))
        ans['Jaw-Teeth']['x']=req['Jaw']['x']
        for i in range(len(req['Teeth']['y'])):
            ans['Teeth-Lips']['y'].append(-abs(req['Teeth']['y'][i]-req['Lips']['y'][TeethLipsIter+i]))
        ans['Teeth-Lips']['x']=req['Teeth']['x']
        '''
        ans={'Jaw-Teeth':[],
             'Teeth-Lips':[],
             'x':[]
        }
        JawTeethIter=self.options['Jaw']['window']+self.options['Jaw']['shift']-self.options['Teeth']['window']-self.options['Teeth']['shift']
        TeethLipsIter=self.options['Teeth']['window']+self.options['Teeth']['shift']-self.options['Lips']['window']-self.options['Lips']['shift']
        for i in range(len(req['Jaw']['y'])):
            ans['Jaw-Teeth'].append(abs(req['Jaw']['y'][i]-req['Teeth']['y'][JawTeethIter+i]))        
            ans['Teeth-Lips'].append(-abs(req['Teeth']['y'][JawTeethIter+i]-req['Lips']['y'][JawTeethIter+TeethLipsIter+i]))
        ans['x']=req['Jaw']['x']
        
        return ans
            
            
    def pltShow(self,showOnlyIndex,drawFig):
        ans=None
        req=[]
        row=1
        if not showOnlyIndex:
            
            row=2
            req.append({
                'vtype':'OCHL',
                'df':self.base_df,
                'row':1,
                'col':1,
                'name':'OHCL'

            })
            

        req.append({
            'vtype':'Bars',
            'df':pd.DataFrame(
                {'Jaw-Teeth':self.ans['Jaw-Teeth'],
                 'Teeth-Lips':self.ans['Teeth-Lips'],
                 'date':self.ans['x'].to_list()}
            ) ,
            'row':row,
            'col':1,
            'name':'Gator',
            'colorType':'diffAbs'
            
        })

        
        self.figDict=req
        ans = market_trade.core.CoreDraw.coreDraw(req,drawFig)
        return ans