import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Button, Input } from 'react-bootstrap';

import { importData } from '../actions';
import { toSeconds, toHHMMSS } from '../utils/convertTime';

@connect(state => ({ commuteData: state.commuteData }))
export default class CommuteDataInput extends Component {
  constructor(props) {
    super(props);
    this.calculateStatistics = this.calculateStatistics.bind(this);
    this.handleCommuteInput = this.handleCommuteInput.bind(this);
    this.processCSV = this.processCSV.bind(this);
  }

  processCSV(commuteData) {
    let [ headersRaw, ...rowsRaw ] = commuteData.split('\n');
    let headers = headersRaw.split(',');
    rowsRaw = rowsRaw.map(row => (row.split(',')));

    return { headers, rowsRaw };
  }

  calculateStatistics(commuteData) {
    let { headers, rowsRaw } = this.processCSV(commuteData);

    const rowLength = rowsRaw.length
      ? Math.max(rowsRaw[0].length - 2, 0)
      : 0;

    // holds all row data
    let rows = [];

    // holds which lane was taken to commute
    let lanes = {};

    // holds average of times for each column
    let averageTimes = new Array(rowLength).fill(0);

    // holds all times for each time column
    let colTimes = [];
    for (var i = 0; i < rowLength - 1; ++i) {
      colTimes.push([]);
    }

    let rowTimes = [];

    // holds total time for each row (in hours)
    let rowTimesLineData = [];

    // holds longest commute time
    let longestCommute = { 
      index: -1,
      value: 0
    };

    // holds shortest commute time
    let shortestCommute = {
      index: -1,
      value: Number.MAX_SAFE_INTEGER
    };

    let rowTime;
    let lapTime;
    let date;
    let lane;
    let averageTime;
    let year, month, day;
    let rowDate;

    rowsRaw.map((row, index) => {
      [ date, lane, ...averageTime ] = row;
      [ month, day, year ] = date.split('/');
      rowDate = new Date(year, month, day);

      rowTime = 0;

      for (let i = 0; i < rowLength; ++i) {
        lapTime = toSeconds(averageTime[i]);

        averageTimes[i] += lapTime;

        if (i > 0) {
          colTimes[i-1].push({
            x: rowDate,
            y: lapTime / 60
          });
        }

        if (i > 1) {
          rowTime += lapTime;
        }
      }

      lanes[lane] ? ++lanes[lane] : lanes[lane] = 1;
      rowTimes.push(toHHMMSS(rowTime));
      rowTimesLineData.push({
        x: rowDate,
        y: rowTime / 3600
      });
      rows.push(row);

      if (rowTime > longestCommute.value) {
        longestCommute.index = index;
        longestCommute.value = rowTime;
      }

      if (rowTime < shortestCommute.value) {
        shortestCommute.index = index;
        shortestCommute.value = rowTime;
      }
    });

    longestCommute.value = toHHMMSS(longestCommute.value);
    shortestCommute.value = toHHMMSS(shortestCommute.value);

    averageTimes = averageTimes.map(averageTime => (
      toHHMMSS(averageTime / rowsRaw.length)
    ));

    rowTimes.forEach((row, index) => {
      rows[index].push(row);
    });

    headers.push('Total');

    return {
      rows,
      headers,
      lanes,
      rowTimes,
      rowTimesLineData,
      colTimes,
      averageTimes,
      longestCommute,
      shortestCommute
    }
  }

  handleCommuteInput(e) {
    const { dispatch } = this.props;
    let commuteData = document.getElementById('commute-data-text').value;
    let stats = this.calculateStatistics(commuteData);

    dispatch(importData(stats));

    window.location.assign('/#/statistics');
  }

  render() {
    return (
      <div>
        <Input id="commute-data-text" type="textarea" rows="30" placeholder="Paste CSV here" />
        <Button onClick={ this.handleCommuteInput } bsStyle="primary">Import CSV Data</Button>
      </div>
    );
  }
}
@@ -1344,32 +1344,8 @@
  %7D%0A%0A
-    let rowTimes = %5B%5D;%0A%0A
     
@@ -1388,16 +1388,16 @@
  hours)%0A
+
     let 
@@ -1942,17 +1942,16 @@
 , day);%0A
-%0A
       ro
@@ -2345,48 +2345,9 @@
  1;%0A
-      rowTimes.push(toHHMMSS(rowTime));
 %0A
+
     
@@ -2444,19 +2444,43 @@
 ws.push(
-row
+%5B...row, toHHMMSS(rowTime)%5D
 );%0A%0A    
@@ -2980,85 +2980,8 @@
 );%0A%0A
-    rowTimes.forEach((row, index) =%3E %7B%0A      rows%5Bindex%5D.push(row);%0A    %7D);%0A%0A
     
@@ -3044,16 +3044,16 @@
 eaders,%0A
+
       la
@@ -3061,24 +3061,8 @@
 es,%0A
-      rowTimes,%0A
     

combine row and rowTimes