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


class Alligator:

    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 getMA(self,keyAns):
        ans=None
        op={'dataType':'ohcl',
                'action':'findMean',
                'actionOptions':{'MeanType':self.options[keyAns]['MeanType'],
                                 'valueType':self.options['valueType'],
                                 'window':self.options[keyAns]['window']}
            }
        ans=CoreTraidMath.CoreMath(self.base_df,op).ans
        
        return ans
    
    def getAns(self):
        ans={'Jaw':{},
             'Teeth':{},
             'Lips':{}
            }
        
        
        for i in ans.keys():
            ma=self.getMA(i)
            ans[i]['y']=ma[:len(ma)-self.options[i]['shift']]
            ans[i]['x']=self.base_df['date'][self.options[i]['window']+self.options[i]['shift']-1:]


        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':'Scatter',
            'df':pd.DataFrame(
                {'value':self.ans['Jaw']['y'],'date':self.ans['Jaw']['x']}) ,
            'row':row,
            'col':1,
            'name':'Jaw'
            
        })
        req.append({
            'vtype':'Scatter',
            'df':pd.DataFrame(
                {'value':self.ans['Teeth']['y'],'date':self.ans['Teeth']['x']}) ,
            'row':row,
            'col':1,
            'name':'Teeth'
            
        })
        req.append({
            'vtype':'Scatter',
            'df':pd.DataFrame(
                {'value':self.ans['Lips']['y'],'date':self.ans['Lips']['x']}) ,
            'row':row,
            'col':1,
            'name':'Lips'
            
        })
        
        self.figDict=req
        ans = market_trade.core.CoreDraw.coreDraw(req,drawFig)
        return ans