# -*- coding: utf-8 -*-

import math
import random
import hashlib
import numpy as np
from OUEdisturb import SingleDataEncode
from NIndexOUE import NIndexJudgeLocation,SUMlist

def RapporParams(epsilon):
    """计算OUE法扰动概率"""
    # epsilon 隐私预算
    
    temp_data=math.pow(math.e,epsilon*0.5)
    prob_p = float(temp_data)/(temp_data+1)
    prob_q = 1.0/(temp_data+1)
    
    return [prob_p,prob_q]

def NIndexDisturbSingleData(single_user_data_index,rappor_params,grid_number):
    """扰动单个用户数据"""
    # single_user_data_index 单个用户数据索引
    # rappor_params rappor法扰动概率
    # grid_number 网格个数
    
    prob_p=rappor_params[0]
    prob_q=rappor_params[1]
    
    noise_single_user_data=[0 for _ in xrange(grid_number)]
    
    for i in xrange(grid_number):
        temp_number=random.random()
        if temp_number < prob_q: 
            noise_single_user_data[i]=0
        else:
            noise_single_user_data[i]=1
    
    temp_number=random.random()
    if temp_number < prob_p: 
        noise_single_user_data[single_user_data_index]=1
    else:
        noise_single_user_data[single_user_data_index]=0
    
    return noise_single_user_data

def NIndexRAPPORDAAllData(all_user_data,x_coordinate,y_coordinate,rappor_params,grid_number):
    """扰动所有用户数据，并求加和值"""
    # all_user_data 所有用户数据列表
    # x_coordinate x轴区间范围，形式为[[a,b],[c,d],[]]
    # y_coordinate y轴区间范围，形式为[[a,b],[c,d],[]]
    # rappor_params rappor法扰动概率
    # grid_number 网格个数
    
    noise_aggregate_all_data=[0 for _ in xrange(grid_number)] # 噪声数据加和值
    
    for i in all_user_data:
        single_user_data_index=NIndexJudgeLocation(i,x_coordinate,y_coordinate)
        noise_single_user_data=NIndexDisturbSingleData(single_user_data_index,rappor_params,grid_number)
        noise_aggregate_all_data=SUMlist(noise_single_user_data,noise_aggregate_all_data)
    
    return noise_aggregate_all_data





