import React from 'react';
import Bar from './ResultsBar';

import Rebase from 're-base';
const base = Rebase.createClass('https://pluto-poll.firebaseio.com');

export default class Results extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      intervalID: undefined,
    };
  }

  componentDidMount() {
    this.ref = base.bindToState('/', {
      context: this,
      state: 'data',
      asArray: true,
    });
    this.checkTime();
  }

  componentWillUnmount() {
    base.removeBinding(this.ref);
    clearInterval(this.state.intervalID);
  }

  checkTime() {
    const intervalID = setInterval(() => {
      const date = new Date();
      if (date.getHours() === 3) {
        const newData = this.state.data;
        for (const item of newData) {
          item.votes = 0;
        }
        this.setState({ data: newData });
      }
    }, 3600000);
    this.setState({ intervalID });
  }

  calcPercentage(votes) {
    const totalVotes = this.state.data
      .map(item => parseInt(item.votes, 10))
      .reduce((a, b) => a + b);
    if (totalVotes === 0) {
      return '0%';
    }
    return Math.round((votes / totalVotes) * 100) + '%';
  }

  render() {
    return (
      <div className="results">
        {this.state.data.map((item, index) =>
          <Bar
            key={index}
            candidate={item.description}
            votes={item.votes}
            percentage={this.calcPercentage(item.votes)}
          />
        )}
      </div>
    );
  }
}

@@ -250,16 +250,17 @@
 props);%0A
+%0A
     this
@@ -321,16 +321,167 @@
 ,%0A    %7D;
+%0A%0A    this.setTimer = this.setTimer.bind(this);%0A    this.initialReset = this.initialReset.bind(this);%0A    this.resetVotes = this.resetVotes.bind(this);
 %0A  %7D%0A%0A  
@@ -526,14 +526,12 @@
 ase.
-bindTo
+sync
 Stat
@@ -602,16 +602,47 @@
 : true,%0A
+      then: this.initialReset,%0A
     %7D);%0A
@@ -650,25 +650,24 @@
    this.
-check
+set
 Time
+r
 ();%0A  %7D%0A
@@ -781,17 +781,16 @@
 %0A%0A  
-check
+set
 Time
+r
 () %7B
@@ -874,154 +874,473 @@
     
-if (date.getHours() === 3) %7B%0A        const newData = this.state.data;%0A        for (const item of
+const day = date.getDay();%0A%0A      const resetDate = new Date(this.state.data%5B0%5D.reset);%0A      const resetDay = resetDate.getDay();%0A%0A      if (day !== resetDay) %7B%0A        this.resetVotes(date);%0A      %7D%0A    %7D, 600000);%0A    this.setState(%7B intervalID %7D);%0A  %7D%0A%0A  initialReset() %7B%0A    const date =
  new
+ 
 Dat
-a) %7B
+e();
 %0A    
-      item.votes = 0;%0A        %7D%0A  
+const day = date.getDay();%0A%0A    const resetDate = new Date(this.state.data%5B0%5D.reset);%0A    const resetDay = resetDate.getDay();%0A%0A    if (day !== resetDay) %7B%0A
     
@@ -1350,32 +1350,125 @@
 his.
+re
 set
-State(%7B data:
+Votes(date);%0A    %7D%0A  %7D%0A%0A  resetVotes(date) %7B%0A    const newData = this.state.data;%0A    for (const item of
  newData
  %7D);
@@ -1467,37 +1467,75 @@
 Data
- %7D);
+) %7B
 %0A      
-%7D%0A    %7D, 3600000);
+item.votes = 0;%0A      item.reset = date.toString();%0A    %7D
 %0A   
@@ -1547,34 +1547,37 @@
 .setState(%7B 
-intervalID
+data: newData
  %7D);%0A  %7D%0A%0A  

add initial reset and reset timer to results