google.charts.load('current', {'packages':['gauge','corechart']});
google.charts.setOnLoadCallback(drawSPMChart);
google.charts.setOnLoadCallback(drawSpeedChart);
google.charts.setOnLoadCallback(drawPowerChart);
google.charts.setOnLoadCallback(drawHRChart);
google.charts.setOnLoadCallback(drawStrokeAngleChart);

var spmdata = [
  ['Label', 'Value'],
  ['SPM', 21],
];

var speeddata = [
  ['Label', 'Value'],
  ['V m/s', 0],
];

var powerdata = [
  ['Label','Value'],
  ['PWR',150],
]

var hrdata = [
  ['Label','Value'],
  ['HR',110],
]

var angledata = [
  ['Angle', 'deg'],
  ['slip',    10 ],
  ['load',      50],
  ['unload',  50],
  ['wash', 10],
  ['recovery',240 ]
]

var anglesoptions = {
  title: 'Stroke Angles',
  legend: 'none',
  pieHole: 0.5,
  chartArea: { width: "100%" },
  pieStartAngle: 35,
  pieSliceText: 'value',
  pieSliceTextStyle: {color:'black',fontSize:12},
  slices: {
    0: { color: 'lightblue' },
    1: { color: 'lightgreen' },
    2: { color: 'yellow'},
    3: { color: 'orange'},
    4: { color: 'transparent', textStyle: {color:'transparent'}}
  }
};


var hroptions = {
  min: 100, max: 200,
  width: 400, height: 120,
  greenFrom: 100, greenTo: 135,
  yellowFrom: 135,yellowTo: 157,
  redFrom: 157, redTo: 200,
  minorTicks: 5
};


var spmoptions = {
  min:0, max: 50,
  width: 400, height: 120,
  greenFrom: 20, greenTo: 30,
  yellowFrom: 30,yellowTo: 40,
  redFrom: 40, redTo: 50,
  majorTicks: ['0','10','20','30','40','50'],
  minorTicks: 10
};

var speedoptions = {
  min:0, max: 6,
  width: 400, height: 120,
  greenFrom: 2, greenTo: 4,
  yellowFrom: 4,yellowTo: 5,
  redFrom: 5, redTo: 6,
  majorTicks: ['0','1','2','3','4','5','6'],
  minorTicks: 10
};

var poweroptions = {
  min: 0, max: 1000,
  width: 400, height: 120,
  greenFrom: 100, greenTo: 200,
  yellowFrom: 200,yellowTo: 400,
  redFrom: 400, redTo: 1000,
  majorTicks: ['0','200','400','600','800','1000'],
  minorTicks: 5
};


var dataspm = null;
var dataspeed = null;
var spmchart = null;
var speedchart = null;
var powerchart = null;
var datapower = null;
var dataangles = null;

// SPM chart
function drawSPMChart() {
  console.log('first draw SPM chart');
  dataspm = new google.visualization.arrayToDataTable(spmdata);


  try {
  spmchart = new google.visualization.Gauge(document.getElementById('basic_spm'));
  spmchart.draw(dataspm,spmoptions);
} catch(err) {

}
  // spmchart.draw(data, spmoptions);

  // Define set_basic(values) so that gauges can be set by metricsgroups

};

// Speed Chart
function drawSpeedChart() {
  dataspeed = new google.visualization.arrayToDataTable(speeddata);

  try {
  speedchart = new google.visualization.Gauge(document.getElementById('basic_boatspeed'));
  speedchart.draw(dataspeed,speedoptions);
} catch(err) {

}
}

// Power chart
function drawPowerChart() {
  datapower = new google.visualization.arrayToDataTable(powerdata);
  try {
  powerchart = new google.visualization.Gauge(document.getElementById('forcepower_power'));
  powerchart.draw(datapower,poweroptions)
} catch(err) {

}

}

// HR chart
function drawHRChart() {
  datahr = new google.visualization.arrayToDataTable(hrdata);
  try {
    hrchart = new google.visualization.Gauge(document.getElementById('athlete_hr'));
    hrchart.draw(datahr,hroptions);
  } catch(err) {
    console.log('no hr div');
  }

}

// Stroke angle chart
function drawStrokeAngleChart() {
  dataangles = new google.visualization.arrayToDataTable(angledata);
  try {
    angleschart = new google.visualization.PieChart(document.getElementById('stroke_angles'));
    angleschart.draw(dataangles,anglesoptions);
  } catch(err) {
    console.log('no angles div');
  }

}

function set_basic() {
  dataspm.setCell(0,1,spm_now);
  spmchart.draw(dataspm, spmoptions);

  dataspeed.setCell(0,1,boatspeed_now);
  speedchart.draw(dataspeed,speedoptions);
}

function set_athlete() {
  datahr.setCell(0,1,hr_now);
  try {
    hrchart.draw(datahr,hroptions);
  } catch(err) {}
}

function set_stroke() {
  var piestartangle = 90+catch_now;
  var load = Math.max(-catch_now-slip_now+peakforceangle_now);
  var unload = Math.max(finish_now-wash_now-peakforceangle_now) ;
  var recovery = Math.max(360+catch_now-finish_now);
  // console.log('load ',load,'; unload ',unload,'; recovery ',recovery,'; pie start angle ',piestartangle);
  dataangles.setCell(0,1,slip_now);
  dataangles.setCell(1,1,load);
  dataangles.setCell(2,1,unload);
  dataangles.setCell(3,1,wash_now);
  dataangles.setCell(4,1,recovery);
  anglesoptions.pieStartAngle = piestartangle;
  try {
    angleschart.draw(dataangles,anglesoptions);
  } catch(err) {
    console.log('failed: ',err);
  }
}

function set_forcepower() {
 datapower.setCell(0,1,power_now);
 try {
 powerchart.draw(datapower,poweroptions);
} catch(err) {}
}
