import React, { Component, PropTypes } from 'react';
import {districtColors, partyData} from './Constants';
import {contiguous} from './util';

class DistrictRow extends Component {
  districtName(id) {
    if (id === 0) {
      return "Unassigned";
    } else {
      return `District ${id}`;
    }
  }

  render () {
    const {id, size, idealSize, parties, winner, precincts} = this.props;
    let correct = true;
    if (idealSize) {
      correct = correct && contiguous(precincts);
      correct = correct && size === idealSize;
    } else {
      correct = correct && size === 0;
    }
    return <tr style={{backgroundColor: districtColors[id]}}>
      {/* TODO: tooltip saying why */}
      <td>{correct ? "✔" : "❌"}</td>
      <th>{this.districtName(id)}</th>
      <td>{size}{idealSize && `/${idealSize}`}</td>
      {/* TODO: colors; generalize */}
      <td>{winner === undefined ? "" :
           winner === null ? "tie" : partyData[winner].name}</td>
      <td>{parties.R} {partyData.R.name}/{parties.D} {partyData.D.name}</td>
    </tr>
  }
}

DistrictRow.propTypes = {
  id: PropTypes.number.isRequired,
  size: PropTypes.number.isRequired,
  idealSize: PropTypes.number,
  parties: PropTypes.objectOf(PropTypes.number),
  winner: PropTypes.string,
}

export default DistrictRow;

@@ -398,22 +398,23 @@
 let 
+in
 correct
- = true
+Reason
 ;%0A  
@@ -442,87 +442,202 @@
     
-correct = correct && contiguous(precincts);
+if (!contiguous(precincts)) %7B%0A        incorrectReason = %22District is not contiguous%22;%0A      %7D else if (size %3E idealSize) %7B
 %0A      
+  in
 correct
- = correct && 
+Reason = %22District too large%22;%0A      %7D else if (
 size 
-===
+%3C
  ide
@@ -646,61 +646,168 @@
 Size
-;%0A    %7D else %7B%0A      correct = correct && size === 0;
+) %7B%0A        incorrectReason = %22District too small%22;%0A      %7D%0A    %7D else %7B%0A      if (size !== 0) %7B%0A        incorrectReason = %22Not all precincts assigned%22;%0A      %7D
 %0A   
@@ -923,28 +923,60 @@
  %3Ctd
-%3E%7B
+ title=%7BincorrectReason%7D%3E%7Bin
 correct
+Reason
  ? %22
-%E2%9C%94
+%E2%9D%8C
 %22 : %22
-%E2%9D%8C
+%E2%9C%94
 %22%7D%3C/

Add tooltips to incorrect districts