var pl, buildColorBar, listTyponLabel, listEnMonthTbl, listEnTyphoonTbl, sampleData, listWitQuery, listQuery, listTitle, lisGoogleQuery, Wit2QTbl, Google2QTbl, queryTitleTbl, loadColorBar, slice$ = [].slice;
pl = require("prelude-ls");
buildColorBar = function(){
  var m, splitXY, build, i$;
  m = {};
  m.xFactor = null;
  m.yFactor = null;
  m.data = [];
  m.colorScheme = "Oranges";
  m.height = 50;
  m.marginH = 2;
  m.width = 80;
  m.quantitativeY = true;
  m.specialOrder = null;
  m.displayUnit = 1;
  m.keyTbl = null;
  m.headerTbl = null;
  m.chartTitle = "";
  m.sr = null;
  m.rowTotal = null;
  splitXY = function(it){
    var s;
    s = it.split("_");
    return {
      "x": +s[0],
      "y": m.quantitativeY
        ? +s[1]
        : s[1]
    };
  };
  build = function(){
    var getLength, sclColor, comparator, sortingObject, sortedObj, sortedKey, fixedHeight, ggl, rectXOffset, sclBar, svg, sclY, txtStyle, format1000, ttlStyle;
    getLength = function(it){
      return (~~it).toString().length;
    };
    m.sr = m.data;
    m.rowTotal = m.data;
    m.displayUnit = Math.pow(10, getLength(
    partialize$.apply(d3, [d3.quantile, [void 8, 0.75], [0]])(
    pl.map(function(it){
      return it.value;
    })(
    m.sr))) - 2);
    sclColor = d3.scale.quantile().domain(pl.map(function(it){
      return it.value;
    })(
    m.sr)).range(pl.drop(2)(
    pl.reverse(
    colorbrewer[m.colorScheme][9])));
    comparator = function(a, b){
      if (m.specialOrder.indexOf(a.key) > m.specialOrder.indexOf(b.key)) {
        return 1;
      } else if (m.specialOrder.indexOf(a.key) < m.specialOrder.indexOf(b.key)) {
        return -1;
      } else {
        return 0;
      }
    };
    sortingObject = function(it){
      return pl.reverse(
      pl.sortBy(function(it){
        return it.value;
      })(
      it));
    };
    if (m.quantitativeY) {
      sortedObj = m.rowTotal;
    } else {
      if (m.specialOrder === null) {
        sortedObj = sortingObject(
        m.rowTotal);
      } else {
        sortedObj = pl.sortWith(comparator)(
        m.rowTotal);
      }
    }
    sortedKey = pl.map(function(it){
      return it.key;
    })(
    sortedObj);
    fixedHeight = sortedKey.length * (m.marginH + m.height);
    ggl = {};
    ggl.margin = {
      top: 60,
      left: 10,
      right: 10,
      bottom: 10
    };
    ggl.w = 800 - ggl.margin.left - ggl.margin.right;
    ggl.h = fixedHeight;
    rectXOffset = 400;
    sclBar = d3.scale.linear().domain([
      0, d3.max(m.sr, function(it){
        return it.value;
      })
    ]).range([0, ggl.w - rectXOffset]);
    svg = d3.select("body").selectAll(".interaction").append("svg").attr({
      "width": ggl.w + ggl.margin.left + ggl.margin.right,
      "height": ggl.h + ggl.margin.top + ggl.margin.bottom,
      "class": "barsvg"
    }).append("g").attr({
      "transform": "translate(" + ggl.margin.left + "," + ggl.margin.top + ")"
    });
    sortedKey = pl.map(function(it){
      return it.key;
    })(
    sortedObj);
    fixedHeight = sortedKey.length * (m.marginH + m.height);
    sclY = d3.scale.ordinal().domain(sortedKey).rangeBands([10, fixedHeight]);
    txtStyle = function(it){
      return it.style({
        "fill": function(it){
          return sclColor(it.value);
        },
        "font-family": "Exo 2, monospace",
        "font-size": m.height
      });
    };
    format1000 = d3.format("0,000");
    svg.selectAll(".valuecell").data(sortedObj).enter().append("text").attr({
      "x": 90,
      "y": function(it, i){
        return sclY(
        it.key);
      }
    }).call(txtStyle).style({
      "text-anchor": "end",
      "class": "valuecell"
    }).text(function(it){
      return format1000(~~(it.value / m.displayUnit));
    });
    svg.selectAll(".keycell").data(sortedObj).enter().append("text").attr({
      "x": 100,
      "y": function(it, i){
        return sclY(
        it.key);
      }
    }).call(txtStyle).style({
      "text-anchor": "start",
      "class": "keycell"
    }).text(function(it){
      if (m.keyTbl !== null) {
        return m.keyTbl[it.key];
      } else {
        return it.key;
      }
    });
    svg.selectAll(".rectcell").data(sortedObj).enter().append("rect").attr({
      "x": rectXOffset,
      "y": function(it, i){
        return sclY(
        it.key) - (m.height - 15);
      },
      "width": 0,
      "height": m.height - 10
    }).style({
      "class": "rectcell",
      "fill": "none",
      "stroke-width": "2px",
      "stroke": function(it){
        return sclColor(it.value);
      }
    }).transition().duration(1500).attr({
      "width": function(it, i){
        return sclBar(
        it.value);
      }
    });
    ttlStyle = function(it){
      return it.style({
        "fill": "white",
        "font-family": "Exo 2, monospace"
      });
    };
    svg.append("text").attr({
      "x": ggl.w - ggl.margin.left - ggl.margin.right,
      "y": -40
    }).style({
      "text-anchor": "end"
    }).call(ttlStyle).text(function(){
      return "unit: " + format1000(m.displayUnit) + " NT$";
    });
    return svg.append("text").attr({
      "x": 10,
      "y": -40
    }).style({
      "text-anchor": "start"
    }).call(ttlStyle).text(m.chartTitle);
  };
  for (i$ in m) {
    (fn$.call(this, i$));
  }
  return build;
  function fn$(it){
    build[it] = function(v){
      m[it] = v;
      return build;
    };
  }
};
listTyponLabel = ["undefined", "minus5", "minus4", "minus3", "minus2", "minus1", "typhoon", "plus1", "plus2", "plus3", "plus4", "plus5"];
listEnMonthTbl = pl.listsToObj([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], ["Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]);
listEnTyphoonTbl = pl.listsToObj(listTyponLabel, ["Normal", "5 day before", "4 day before", "3 day before", "2 day before", "1 day before", "TYPHOON", "1 day after", "2 day after", "3 day after", "4 day after", "5 day after"]);
sampleData = function(){
  return [
    {
      "key": "Jan.",
      "value": 10
    }, {
      "key": "Feb.",
      "value": 20
    }, {
      "key": "Mar.",
      "value": 30
    }, {
      "key": "Apr.",
      "value": 100 * Math.random()
    }
  ];
};
listWitQuery = ["per_type", "per_market", "per_year", "per_typhoon", "per_month"];
listQuery = ["getsbtype", "getMarket", "getYear", "getTyphoonday", "getMonth"];
listTitle = ["By Type", "By Market", "By Year", "By Typhoon", "By Month"];
lisGoogleQuery = ["分種類", "分市場", "分年份", "分颱風", "分月份"];
Wit2QTbl = pl.listsToObj(listWitQuery, listQuery);
Google2QTbl = pl.listsToObj(lisGoogleQuery, listQuery);
queryTitleTbl = pl.listsToObj(listQuery, listTitle);
loadColorBar = function(fileName, queryName){
  return d3.json("./month_month_computedVeggie/" + fileName + ".json", function(err, jsonData){
    return jsonData.map(function(it, i){
      var qy, hd;
      if (it.yFactor === Google2QTbl[queryName] && it.crrAttr === "middleprice") {
        qy = listQuery.indexOf(queryName) <= 1 ? false : true;
        if (queryName === "getTyphoonday") {
          hd = listEnTyphoonTbl;
        } else if (queryName === "getMonth") {
          hd = listEnMonthTbl;
        } else {
          hd = null;
        }
        return buildColorBar().data(it["rowTotal"]).chartTitle(queryTitleTbl[queryName]).quantitativeY(qy).keyTbl(hd)();
      }
    });
  });
};
function partialize$(f, args, where){
  var context = this;
  return function(){
    var params = slice$.call(arguments), i,
        len = params.length, wlen = where.length,
        ta = args ? args.concat() : [], tw = where ? where.concat() : [];
    for(i = 0; i < len; ++i) { ta[tw[0]] = params[i]; tw.shift(); }
    return len < wlen && len ?
      partialize$.apply(context, [f, ta, tw]) : f.apply(context, ta);
  };
}