var basehr = 0;  //静态心率
var age = 0;
var sex = 0;     //1: 男  2: 女
var weight = 0;

//运动强度权重
// var acWeight = {'39': 10, '54': 20, '69': 40, '79': 60, '89': 80, '100': 100};
var acWeight = [0, 10, 20, 40, 60, 80, 100];

//运动强度在各区间的占比
// var acMap = {'39': 0, '54': 0, '69': 0, '79': 0, '89': 0, '100': 0};
var acMap = [0, 0, 0, 0, 0, 0, 0];

//运动数据
var sportInfo = {
    intensity: 0,   //运动强度
    intensityLevel: 0,   //运动强度等级
    intensityLevelString: '',   //运动强度等级说明
    intensityLevelColor: '',   //运动强度等级颜色
    time: 0,        //当前时刻的时长 毫秒
    total_time: 0,  //总运动时间 毫秒
    cle: 0,         //当前时刻的卡路里
    total_cle: 0,   //总卡路里
    ei: 0,          //EI
};

// 计算各项运动数据
//time: 当前时刻的时长 毫秒  hr: 当前心率
function getSportInfo(time, hr) {
    sportInfo.intensity = computeSpIntensity(hr);  //计算运动强度 0-100
    sportInfo.intensityLevel = computeSpIntensityLevel(sportInfo.intensity);  //计算运动强度等级
    sportInfo.intensityLevelString = getSpIntensityLevelString(sportInfo.intensityLevel);
    sportInfo.intensityLevelColor = getSpIntensityLevelColor(sportInfo.intensityLevel);
    sportInfo.time = time;
    sportInfo.total_time += sportInfo.time;
    sportInfo.cle = computeCAL(time, hr);
    sportInfo.total_cle += sportInfo.cle;

    computeAcMap(sportInfo.intensityLevel);
    sportInfo.ei = computeEi(sportInfo.total_cle, sportInfo.total_time);

    // console.log("time " + sportInfo.time);
    // console.log("total_time " + sportInfo.total_time);  
    // console.log("cle " + sportInfo.cle);
    // console.log("total_cle " + sportInfo.total_cle);
    // console.log("ei " + sportInfo.ei);

    return sportInfo;
}

function setBaseInfo(_age, _sex, _weight, _basehr) {
    age = _age;
    sex = _sex;
    weight = _weight;
    basehr = _basehr;
}

/*
* 计算运动强度 0-100
* 目标心率 = {(220-年龄)-静态心率} × 运动强度% + 静态心率
* 运动强度% = (当前心率 - 静态心率) / {(220-年龄)-静态心率}
*/
function computeSpIntensity(hr) {
    let intensity = 100 * (hr - basehr) / (220 - age - basehr);
    if (intensity > 0)
        return Math.round(intensity);
    else
        return 0;
}

/*
* 计算运动强度等级
 * spIntensity: 运动强度
 * 强度等级 1 (0%-39%) 激活放松
 * 强度等级 2 (40%-54%) 动态热身
 * 强度等级 3 (55%-69%) 脂肪燃烧
 * 强度等级 4 (70%-79%) 糖分消耗
 * 强度等级 5 (80%-89%) 心肺训练
 * 强度等级 6 (>=90%) 峰值锻炼
*/
function computeSpIntensityLevel(spIntensity) {
    if (spIntensity >= 0 && spIntensity <= 39) return 1;
    else if (spIntensity >= 40 && spIntensity <= 54) return 2;
    else if (spIntensity >= 55 && spIntensity <= 69) return 3;
    else if (spIntensity >= 70 && spIntensity <= 79) return 4;
    else if (spIntensity >= 80 && spIntensity <= 89) return 5;
    else if (spIntensity >= 90) return 6;
    else return 0;
}

// spIntensityLevel: 运动强度等级
function getSpIntensityLevelString(spIntensityLevel) {
    let str = "";
    switch (spIntensityLevel) {
        case 0:
            str = "暂无数据";
            break;
        case 1:
            str = "激活放松";
            break;
        case 2:
            str = "动态热身";
            break;
        case 3:
            str = "脂肪燃烧";
            break;
        case 4:
            str = "糖分消耗";
            break;
        case 5:
            str = "心肺训练";
            break;
        case 6:
            str = "峰值锻炼";
            break;
        default:
            str = "未知状态";
    }
    return str;
}

// spIntensityLevel: 运动强度等级
function getSpIntensityLevelColor(spIntensityLevel) {
    if (spIntensityLevel == 0) return "#028FE1";
    else if (spIntensityLevel == 1) return "#028FE1";
    else if (spIntensityLevel == 2) return "#6D29FA";
    else if (spIntensityLevel == 3) return "#0AB105";
    else if (spIntensityLevel == 4) return "#B9CB01";
    else if (spIntensityLevel == 5) return "#EA8813";
    else if (spIntensityLevel == 6) return "#D11122";
    else return "#028FE1";
}

/*
 * 计算总卡路里 (与TAKE数值接近)
 * 男：((-55.0969 +(0.6309×HR) +(0.1988 xW)+(0.2017×A))/ 4.184) xT
 * 女：((-20.4022 +(0.4472×HR) -(0.1263 xW)+(0.074×A))/ 4.184) xT
 * HR =心率(次/分钟) W =体重(kg) A=年龄(岁) T =练习持续时间(分钟)
 * time: 毫秒
*/
function computeCAL(time, hr) {
    let cal = 0.0
    if (time > 0 && hr > 0) {
        if (1 == sex)   //男
            cal = ((-55.0969 + (0.6309 * hr) + (0.1988 * weight) + (0.2017 * age)) / 4.184) * time / 1000 / 60;
        else    //女
            cal = ((-20.4022 + (0.4472 * hr) - (0.1263 * weight) + (0.074 * age)) / 4.184) * time / 1000 / 60;
    } else {
        cal = 0.0;
    }

    if (cal > 0)
        return cal;
    else
        return 0.0;
}

// 计算运动强度在各区间的占比
// intensityLevel：运动强度等级 1-6
function computeAcMap(intensityLevel) {
    if (intensityLevel >= 0 && intensityLevel <= 6)
        acMap[intensityLevel]++;
}

//计算EI
//total_cle: 总卡路里  total_time: 总运动时间 毫秒
function computeEi(total_cle, total_time) {
    let wi = 0;
    let ei = 0;
    let time = total_time / 1000 / 60;  //转换为分钟
    let sumAcMap = 0;

    for (n=1; n<=6; n++) {
        sumAcMap += acMap[n];
    }

    for (i=1; i<=6; i++) {
        wi = acMap[i] / sumAcMap;
        ei += wi * acWeight[i] * total_cle / time;
    }
    return ei;
}
