#Intialization - creates db and then sends an empty response

s = """{
  "Details": {
    "Username" : "Anonymous",
    "Story"    : "Rabbit Story"
  },
  "Characters": [
    {
      "Name": "Rabbit",
      "Position": "Right",
      "Social"  : 0.3,
      "Emotion": "Happy"
    },

    {
      "Name": "Turtle",
      "Position": "Left",
      "Social"  : 0.2,
      "Emotion": "Sad"
    }
  ]
}
"""

import json
string = json.loads(s)

username = string['Details']['Username']
story    = string['Details']['Story']
num_char = len(string['Characters'])

matrices, closeness_vectors = initialize(num_char)

#Initialize DB
character_id = 0
for elem in string['Characters']:
    #DB[Character objs] puts the following
    
    char_id      = character_id
    name         = elem['Name']
    position     = elem['Position']
    socialbility = elem['Social']
    emotion      = translate_to_num(elem['Emotion'])
    personality  = matrices[character_id]
    impact       = matrices[character_id + num_char]
    c_vector     = closeness_vectors[character_id]
        
    print char_id
    print name
    print position
    print socialbility
    print emotion
    print personality
    print impact
    print c_vector
    print
    
    character_id += 1
        
return generate_response(username,story,0,0)


#Update Text Request
#update text in db
#send response back

s = {
  "Details": {
    "Story": "Rabbit Story",
    "Username": "Anonymous",
    "Frame_start": 1,
    "Frame_end": 1
  },

  "Frames": [ 
    {
      "Characters":[
        {
          "Name": "Rabbit",
          "Text": "Hello Turtle1"
        },

        {
          "Name": "Turtle",
          "Text": "Hello Rabbit1"
        }
      ]
    },

    {
      "Characters":[
        {
          "Name": "Rabbit",
          "Text": "Hello Turtle2"
        },
        {
          "Name": "Turtle",
          "Text": "Hello Rabbit2"
        }
      ]
    }
  ]
}


"""
Explanation: Few things to keep aware of. 
People acessing things at the same time, take the earlier user's request, then discard the other guys request and then continue the chain.
End of explanation
"""


import json
string = json.loads(s)

username   = string['Details']['Username']
story      = string['Details']['Story']
start      = string['Details']['Frame_start']
end        = string['Details']['Frame_end']

#get number of the last frame from db
if last_known_frame < int(end):
    #return malformed JSON
else:
    #return JSON wanted
    for elem in string['Frames']:
        for sub_elem in elem['Characters']:
            char_name = sub_elem['Name']
            text      = sub_elem['Text']
            #puts in db[story][start][char_name][TEXT ENTRY] = text 
            
        start += 1
        if start > end:
            break
        
return generate_response(username,story,start,end)

#continue past Story
#Check for continuing or querying

#continuing
#Samples the markov chain for each character in the frame and then updates the db
#then send the response back

#querying 
#query db and send info w/ text back
s = {
  "Details": {
    "Story": "Rabbit Story",
    "Username": "Anonymous",
    "Frame_start": 1,
    "Frame_end": 3
  }
}


import json
string = json.loads(s)

username   = string['Details']['Username']
story      = string['Details']['Story']
start      = string['Details']['Frame_start']
end        = string['Details']['Frame_end']

#run a for
    #check if frame is in db
        #continue
    #else:
        #pull num of characters in story something like db.story.characterobjs
        
        #for 2 characters chosen at random:
            #sample markov chain for one and update in db
#return generate_response(username, story, start, end)

import numpy as np
import json

total_influence = np.matrix([[0.0, 0.0, 0.0, 0.0],
                                     [0.0, 0.0, 0.0, 0.0],
                                     [0.0, 0.0, 0.0, 0.0],
                                     [0.0, 0.0, 0.0, 0.0]])
s =  json.dumps(total_influence.tolist())
print total_influence


print np.matrix(json.loads(s))

"""
Explanation: create a function that access the db w/ the name of the character and gets its char_id
process
for every frame, update the text in the db nothing markov
if frames out of bound bail
End of explanation
"""


