import pymongo
import time
import json
from conn_mongodb import *


# client = pymongo.MongoClient("mongodb://localhost:27017/")
# client = pymongo.MongoClient("mongodb://admin:readgazer_2022@81.69.98.171:27017/test")
# db = client["ReaderGazer"]  # 或者用db = client.cnnStories


def save_data_simple(email, myfilename,mysecond,transfermsg,freq,seqlist,pagefreq,pagefreqoff):
    # history_col = db.historyInfo
    print("saving!!")
    localtime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
    query = {"email":email,"filename": myfilename,"mysecond":mysecond,"transfermsg":transfermsg,"freq":freq,"seqlist":seqlist,"pagefreq":pagefreq,"pagefreqoff":pagefreqoff,"time":localtime}
    # query = {"email":email,"filename": myfilename,"mysecond":mysecond,"transfermsg":transfermsg,"freq":freq,"seqlist":seqlist,"pagefreq":pagefreq,"pagefreqoff":pagefreqoff,"first_fixation":first_fixation,"avg_fixation":avg_fixation,"back_rate":back_rate,"time":localtime}
    a=history_col.insert_one(query)
    print("save data!")
    # print(historyInfo_col.find_one(query))
    return 'OK'

def save_data(email, myfilename,mysecond,transfermsg,freq,seqlist,pagefreq,pagefreqoff):
    # history_col = db.historyInfo
    print("saving!!")
    localtime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
    avg_fixation=cal_avg_fixation(seqlist,freq)
    back_rate=back_percent(seqlist)
    first_fixation=get_first_fixation(seqlist,freq)
    query = {"email":email,"filename": myfilename,"mysecond":mysecond,"transfermsg":transfermsg,"freq":freq,"seqlist":seqlist,"pagefreq":pagefreq,"pagefreqoff":pagefreqoff,"first_fixation":first_fixation,"avg_fixation":avg_fixation,"back_rate":back_rate,"time":localtime}
    # query = {"email":email,"filename": myfilename,"mysecond":mysecond,"transfermsg":transfermsg,"freq":freq,"seqlist":seqlist,"pagefreq":pagefreq,"pagefreqoff":pagefreqoff,"first_fixation":first_fixation,"avg_fixation":avg_fixation,"back_rate":back_rate,"time":localtime}
    a=history_col.insert_one(query)
    print("save data!")
    # print(historyInfo_col.find_one(query))
    return 'OK'

def save_result(email, res,filename):
    userTest_col = db_2.userTestResult
    localtime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
    query = {"email":email,"filename":filename,"result": res,"time":localtime}
    a=userTest_col.insert_one(query)
    print("save result!")
    return 'OK'

def cal_avg_fixation(seqlist,freq):
    # seq=json.loads(seqlist)
    # freq=json.loads(freq)
    seq=seqlist
    avg_fixation=[]
    print(type(seq[0]))
    for i in range(len(freq)):
        avg_fixation_t=[]
        for j in range(len(freq[0])):
            avg_fixation_t.append(0)
        avg_fixation.append(avg_fixation_t)
    for i in range(len(seq)-1):
        avg_fixation[seq[i][0]][seq[i][1]]+=seq[i+1][2]-seq[i][2]
    return str(avg_fixation)

def back_percent(seqlist):
    # seq=json.loads(seqlist)
    seq=seqlist
    ordinary=0
    back=0
    for i in range(len(seq)-1):
        ordinary+=1
        if(seq[i][0]>seq[i+1][0]):
            back+=1
        elif(seq[i][0]==seq[i+1][0] and seq[i][1]>seq[i+1][1]):
            back+=1
    return 1.0*back/ordinary

def get_first_fixation(seqlist,freq):
    # seq=json.loads(seqlist)
    # freq=json.loads(freq)
    seq=seqlist
    first_fixation=[]
    second_fixation=[]
    for i in range(len(freq)):
        first_fixation_t=[]
        second_fixation_t=[]
        for j in range(len(freq[0])):
            first_fixation_t.append(0)
            second_fixation_t.append(0)
        first_fixation.append(first_fixation_t)
        second_fixation.append(second_fixation_t)

    for i in range(len(seq)):
        if(first_fixation[seq[i][0]][seq[i][1]]==0):
            first_fixation[seq[i][0]][seq[i][1]]=seq[i][2]
        elif(first_fixation[seq[i][0]][seq[i][1]]!=0 and second_fixation[seq[i][0]][seq[i][1]]==0):
            second_fixation[seq[i][0]][seq[i][1]]=seq[i][2]
    for i in range(len(second_fixation)):
        for j in range(len(second_fixation[0])):
            if(second_fixation[i][j]!=0):
                second_fixation[i][j]=second_fixation[i][j]-first_fixation[i][j]
    # print(seq[0][2])
    # print(second_fixation)
    return str(second_fixation)
    

def save_accuracy(email, onlinelist,offlinelist,testseqlist,evallist):
    accuracyInfo_col = db.accuracyInfo
    localtime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
    query = {"email":email,"onlinelist": onlinelist,"offlinelist":offlinelist,"testseqlist":testseqlist,"evallist":evallist,"time":localtime}
    a=accuracyInfo_col.insert_one(query)
    return 'OK'

# save_data(1, 1,1,1,1,1,1,1)