import pandas as pd
import datetime
import numpy as np

import market_trade.core.CoreTraidMath as CoreTraidMath
import market_trade.core.CoreDraw as CoreDraw

class coreIndicator():
    def __init__(self,
                 data=pd.DataFrame(),
                 options={},
                 showMode='None',
                ):
        '''
        showMode = None/Ind/PartOf        
        '''
        self.data=data
        self.showMode=showMode
        self.options=options
        self.overlayInd=None #True/False
        self.ans=None
        self.figDict=None
        
    def getAns(self,data=None):
        if type(data)!=type(None):
            self.data=data
        self.ans=self.getCalculate()
        if self.showMode=='Ind' or self.showMode=='PartOf':
            self.figDict=self.getFigDict()
            if self.showMode=='Ind':
                self.getFig()
        return self.ans
    def getFig(self,row=1):
        CoreDraw.coreDraw(self.figDict,True)
    def getCalculate(self):
        return "Error"
    def getFigDict(self):
        return "Error"

class indicatorAgrigator():
    '''
    Тема чисто для отладки
    jj=indicatorAgrigator().runAll([o1,o2],df_candle[:30])
    #jj.createIndFromList([o1,o2])
    #jj.calculateInd(df_candle[:30])
    
    '''
    def __init__(self):
        self.indList=None
        self.data=None
    def createInd(self,classDict):
        return classDict['name'](
                options=classDict['params'],
                showMode=classDict['showMode']
            )
        
        
    def createIndFromList(self,indList):
        self.indList=indList
        ans=[]
        for i in self.indList:
            ans.append(self.createInd(i))
        self.indList=ans
        return ans
    
    def calculateInd(self,data):
        self.data=data
        for i in self.indList:
            #i.getAns(data)
            i.data=self.data
            i.ans=i.getCalculate()
            i.figDict=i.getFigDict()
            #i.getFig()
    def agrigateFig(self):
        req=[[]]
        
        for i in self.indList:
            if i.overlayInd==True:
                req[0].append(i)
            else:
                req.append([i])
        CoreDraw.agrigateFig(req,True)
    def runAll(self,indList,df,needDraw=False):
        self.createIndFromList(indList)
        self.calculateInd(df)
        if needDraw:
            self.agrigateFig()

class ind_BB(coreIndicator):
    
    def getCalculate(self):
        self.overlayInd=True
        ans={}
        opMA={'dataType':'ohcl',
            'action':'findMean',
            'actionOptions':{
                'MeanType':self.options['MeanType'],
                'valueType':self.options['valueType'],
                'window':self.options['window']
            }
        }
        ans['BB']=CoreTraidMath.CoreMath(self.data,opMA).ans
        opSTD={'dataType':'ohcl',
            'action':'findSTD',
            'actionOptions':{'valueType':self.options['valueType'],'window':self.options['window']}
        }
        ans['STD']=CoreTraidMath.CoreMath(self.data,opSTD).ans
        ans['pSTD']=ans['BB']+ans['STD']*self.options['kDev']
        ans['mSTD']=ans['BB']-ans['STD']*self.options['kDev']
        ans['x']=np.array(self.data['date'][self.options['window']-1:].to_list())
        return ans
    def getFigDict(self,row=1):
        req=[]
        
        req.append({
            'vtype':'Scatter',
            'df':pd.DataFrame(
                {'value':self.ans['BB'],'date':self.ans['x']}) ,
            'row':row,
            'col':1,
            'name':'BB'
            
        })
        req.append({
            'vtype':'Scatter',
            'df':pd.DataFrame(
                {'value':self.ans['pSTD'],'date':self.ans['x']}) ,
            'row':row,
            'col':1,
            'name':'pSTD'
            
        })
        req.append({
            'vtype':'Scatter',
            'df':pd.DataFrame(
                {'value':self.ans['mSTD'],'date':self.ans['x']}) ,
            'row':row,
            'col':1,
            'name':'mSTD'
            
        })
        
        return req
    
class ind_OCHL(coreIndicator):
    def getCalculate(self):
        self.overlayInd=True
    def getFigDict(self,row=1):
        req=[]
        
        req.append({
                'vtype':'OCHL',
                'df':self.data,
                'row':1,
                'col':1,
                'name':'OHCL'

        })
        return req