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

class decisionMaking():
    
    def __init__ (self, name=''):
        
        self.name=name
        self.mop={
            'up': pd.DataFrame(),
            'down':pd.DataFrame(),
            'none':pd.DataFrame()
        
        }
        self.value={}
        self.decision=''
        self.real_decision=''
        self.keys=[]
        self.slice_dict={}
        
    def addValue(self, dic_value):
        self.value=dic_value
        self.checkForNew()
        self.setSlice()
        self.getDecision()
        

        
    def checkForNew(self):
        
        if not (list(self.value.keys()) == self.keys):
            self.createNewMop(list(self.value.keys()))

    
    def createNewMop(self,missing_indicators):
        print('reassembly mop')
        new_columns= (missing_indicators)
        
        
        #new_columns=new_columns.append(['value','p'])
        
        
        
        n=len(new_columns)
        start_value=-1
        variator=3
        new_lst=[]
        buf_lst=[]
        for i in range(n):
            buf_lst.append(start_value)


        for i in range(pow(variator,n)):
            new_lst.append(buf_lst.copy())

        for j in range(n):
            for i in range(len(new_lst)):
                dob_iterator=(i // pow(variator,j)) % variator
                new_lst[i][j]=new_lst[i][j] + dob_iterator
                
        
        #print (new_columns)
        self.keys=new_columns
        new_columns = new_columns+['amount']+['percentage']
                
        for i in new_lst:
            i = i.extend([0,0])
            #i = i.append(0)
        
        #print(new_lst)
        #print(new_columns)
        new_df=pd.DataFrame(new_lst,columns=new_columns)
        
        self.mop['up']=pd.DataFrame.from_dict(new_df.to_dict())
        self.mop['down']=pd.DataFrame.from_dict(new_df.to_dict())
        self.mop['none']=pd.DataFrame.from_dict(new_df.to_dict())
        
        
    def setSlice(self):
        
        row_flg=True
        self.slice_dict={}
        for j in self.mop.keys():
            for index, row in self.mop[j].iterrows():
                for key, value in self.value.items():
                    if value != row[key]:
                        #print('fasle ',key,value,row[key])
                        row_flg=False
                        break
                if row_flg:
                    self.slice_dict[j]=dict(row)
                    #print(j,dict(row))
                row_flg=True

    def getDecision (self):
        
        max_value=0
        for key, value in self.slice_dict.items():
            if value['amount'] >= max_value:
                max_value = value['amount']
                self.decision = key
        return self.decision
    
    def setDecision (self,real_decision):
        self.real_decision=real_decision
        self.updMop()
        self.slice_dict[real_decision]['amount']+=1


    def updMop(self):
        
        row_flg=True
        for index, row in self.mop[self.real_decision].iterrows():
            for key, value in self.value.items():
                if value != row[key]:
                    row_flg=False
                    break
            if row_flg:
                #self.slice_dict[j]=dict(row)
                row['amount']=row['amount']+1
            row_flg=True 
        
        
        
    

        
        
        
       
    
    
        
        
