// innerplate.js
// functions for calculating commitment

var weeks=0;
var daily_calories=0;
var desired_weight_loss=0;
var pounds_per_week=0;
var MILLISEC_PER_WEEK= (1000*60*60*24*7);
var MILLISEC_PER_DAY= (1000*60*60*24);
var CALORIES_PER_POUND_LOSS=3500;
var DAYS_PER_WEEK=7;
var MAINTENANCE_CALORIES_PER_POUND_WEIGHT=12;
  
// calculate weeks specified for commitment
function calculateWeeks(){

  end_date=new Date();
  end_date.setFullYear(
      $('commitment_end_date_1i').value,
      $('commitment_end_date_2i').value - 1,
      $('commitment_end_date_3i').value
    );
  
  start_date=new Date();
  start_date.setFullYear(
      $('commitment_start_date_1i').value,
      $('commitment_start_date_2i').value - 1,
      $('commitment_start_date_3i').value
    );
  weeks= (end_date - start_date + MILLISEC_PER_DAY) / MILLISEC_PER_WEEK;
  return weeks.toFixed(1);
}

function calculateDays(){
  return calculateWeeks() * DAYS_PER_WEEK
}

// update weeks element    
function setWeeks(){
  $('weeks').update(calculateWeeks());
}

// set end_date based on users desired weeks (alternate to specifying end_date)
function setDuration(weeks){
  time=MILLISEC_PER_WEEK * weeks;
  end_date = start_date + time;
  window.console.log("end_date=" + end_date);
}

function calculateDesiredWeightLoss() {
  desired_weight_loss= $F('commitment_start_weight') - $F('commitment_goal_weight');
  window.console.log("desired_weight_loss=" + desired_weight_loss );
  return desired_weight_loss;
}

function calculateDailyCalorieDeficit(){
  pounds_per_week=$F("commitment_weight_per_week");
  calories_to_lose=pounds_per_week * CALORIES_PER_POUND_LOSS;
  window.console.log("calories_to_lose=" + calories_to_lose);
  calories_per_day= calories_to_lose / DAYS_PER_WEEK;
  window.console.log("calories_per_day=" + calories_per_day);
  return calories_per_day;
}

function calculateDailyCalorieDeficit2(){
  calories_to_lose=calculateDesiredWeightLoss() * CALORIES_PER_POUND_LOSS;
  window.console.log("calories_to_lose=" + calories_to_lose);
  calories_per_day= calories_to_lose / (calculateWeeks() * DAYS_PER_WEEK);
  window.console.log("calories_per_day=" + calories_per_day);
  return calories_per_day;
}

// only called for changes to goal daily calories
function setGoalWeight(){
  start_weight= $F('commitment_start_weight');
  daily_calorie_margin= $F('commitment_stable_daily_calories')- $F( 'commitment_goal_daily_calories');
  // todo: does this deal with wieght gain initiatives?
  
  days=calculateDays();
  weight_loss = daily_calorie_margin * days / CALORIES_PER_POUND_LOSS;
  
  goal_weight = start_weight - weight_loss;
  
  window.console.log("setGoalWeight=" + goal_weight );
  $('commitment_goal_weight').setValue(goal_weight.toFixed(1));
}

function setDailyCalories(){
  daily_calories= $F('commitment_stable_daily_calories') - calculateDailyCalorieDeficit();
  window.console.log("setDailyCalories=" + daily_calories );
  $('commitment_goal_daily_calories').setValue(daily_calories.toFixed(1));
  setGoalWeight();
}

// only change when entering a specific goal_calories
function setPoundsPerWeek(){
  //daily_calories= $F('commitment_stable_daily_calories') - calculateDailyCalorieDeficit();
  daily_calories= $F('commitment_stable_daily_calories') - $F('commitment_goal_daily_calories');
  pounds_per_week=daily_calories*7/3500.0
  $("commitment_weight_per_week").setValue(pounds_per_week.toFixed(1));
  setGoalWeight();
}

// called when start_weight set. todo: Why is this here? isn't setDailyCalories same?
function setWeightListener(){
  window.console.log("setWeightListener");
  setDailyCalories();
}

// General call to run calcuations where not triggered by blur or change events
function runCalculations(){
  // weeks and daily calories are the two derived fields
  //setWeeks();
  setDailyCalories();
}

// General call to run calcuations where not triggered by blur or change events
function runDateCalculations(){
  // weeks and daily calories are the two derived fields
  setWeeks();
  setGoalWeight();
  // setDailyCalories();
}


function handleStatus(){
  status=$F('commitment_status');
  console.log("handleStatus() called, status=" + status);
  if(status == "open"){
    console.log("hide end weight");
    $j('#end_weight').hide();
  }else if(status == "completed"){
    console.log("show end weight");
    $j('#end_weight').show();
    $j('#commitment_end_weight').val(last_end_weight);
  }else if(status == "cancelled"){
    console.log("show end weight");
    $j('#end_weight').show();
    $j('#commitment_end_weight').val(last_end_weight);
  }
}

function setHandlers(){
  console.log("setHandlers() called");
  $('commitment_end_date_1i').onclick= runDateCalculations;
  $('commitment_end_date_2i').onclick= runDateCalculations;
  $('commitment_end_date_3i').onclick= runDateCalculations;
  $('commitment_start_date_1i').onclick= runDateCalculations;
  $('commitment_start_date_2i').onclick= runDateCalculations;
  $('commitment_start_date_3i').onclick= runDateCalculations;

  $('commitment_status').onchange=handleStatus;
  
  $('commitment_start_weight').onblur=setGoalWeight;
  $('commitment_goal_weight').onblur=setDailyCalories;

  $('commitment_weight_per_week').onblur=setDailyCalories; 

  $('commitment_stable_daily_calories').onblur=setDailyCalories;

  $('commitment_goal_daily_calories').onblur=setPoundsPerWeek; //only one contrained to change pounds per week
    
  runCalculations();
  handleStatus();
}


