import { sumData } from './main.js';

// COLORES
export var m = '#00758d'; // Azul Marino
export var c = '#dc272d'; // Carmesi
export var y = '#f6a800'; // Amarillo oro
export var t = '#5ecbca'; // Turquesa
export var g = '#8a8a8e'; // Gris
export var p = '#7c26cb'; // Purpura electrico
export var lg = '#CDCDCD'; // Gris claro
export var ghc = '#333333'; // Gris default de Highcharts

// Para options.test.js
// data (int): el numero a evaluar
// level (int): el nivel del cuadrante, de este depende los distintos rangos que se aplicaran
export function streetLight(data, level){

  var color = null;
  var tier_1_min; var tier_1_max; var tier_2_min; var tier_2_max; var limit;

  switch(level){

    case 0:
      tier_1_min = 0; tier_1_max = 33; tier_2_min = tier_1_max + 1; tier_2_max = 66;
      limit = tier_2_max + 1;
      break;

    case 1:
      tier_1_min = 0; tier_1_max = 500; tier_2_min = tier_1_max + 1; tier_2_max = 1500;
      limit = tier_2_max + 1;
      break;

    case 2:
      tier_1_min = 0; tier_1_max = 50; tier_2_min = tier_1_max + 1; tier_2_max = 150;
      limit = tier_2_max + 1;
      break;
  }

  if(data >= tier_1_min && data <= tier_1_max){
    color = '#dc272d';
  }
  if(data >= tier_2_min && data <= tier_2_max){
    color = '#f6a800';
  }
  if(data >= limit){
    color = '#00758d';
  }
  return color;
  
}

export function makeChartTest(title, sections, series, real, ppto, level){

  var options = {
    chart: {
      // height: 300
    },
    title: {
      text: title
    },
    tooltip: {
      shared: true,
      enabled: true,
      // crosshairs: true
    },
    xAxis: {
      categories: sections
    },
    yAxis: [{
      title: {
        text: '%',
      },
      plotLines: [{
        zIndex: 1,
        color: 'red',
        width: 2,
        value: 0,
        label: {
          text: "Meta"
        }
      }],
    }, {
      title: { text: null },
      opposite: true
    }],
    exporting: {
      enabled: false
    },
    credits: {
        enabled: false
    },
    series: series
  }

  // Real
  options.series[0].data = real;
  
  // Presupuesto
  options.series[1].data = ppto;
  var sumPpto = sumData(ppto);

  // Acumulado
  options.series[2].data = [sumData(real)]; var acum = options.series[2].data; // Valor acumulado
  options.series[2].color = streetLight(acum, level); // Color
  options.series[2].pointStart = sections.length-1; // Punto que empieza el acumulado

  // Meta
  var valAcum = sumData(ppto);
  options.yAxis[0].plotLines[0].value = valAcum/4;
  
  return options;
}

export function C4_DrawGauge(title, measure, size, series){

  var gap = -(size/5);
  var tick = size/5;

  var meta              = series[0].data[0].y;
  var valorCumplimiento = series[0].data[1].y;

  var colorMeta = series[0].data[1].color = streetLight(valorCumplimiento, 0);

  var gauge = {
    chart: {
      type: 'solidgauge',
      height: size,
    },
    title: {
        text: title+' '+measure,
    },
    exporting:{
      enabled:false,
    },
    pane: {
      center: ['50%', '50%'],
      size: '100%',
      startAngle: -100,
      endAngle: 100,
      rounded:true,
      background: {
          backgroundColor: '#EEE',
          innerRadius: '60%',
          outerRadius: '100%',
          shape: 'arc'
      }
    },
    yAxis: [{
      lineWidth:0,
      minorTickInterval: null,
      min: 0,
      max: 100,
      tickPixelInterval: 400,
      tickWidth: 0,
      labels: {
         y: 16,
         style: {
           color: ghc,
           fontSize: '15px',
           fontWeight: 'bold'
         }
      },
      minPadding:1,
      zIndex: -1,
    }, {
      zIndex: 5,
      linkedTo: 0,
      lineWidth: 2,
      minorTickLength: 0,
      tickPositions: [meta],
      tickLength: tick,
      tickColor: 'red',
      lineColor: colorMeta,
      labels: {
        distance: gap,
        style: {
          color: ghc,
          fontSize: '15px',
          fontWeight: 'bold'
        },
        formatter:function(){
          if(size>=300){
            return this.value + "%";
          }
        }
      }
    }],
    plotOptions: {
        solidgauge: {
            dataLabels: {
                style: {
                  color: ghc,
                  fontSize: '25px',
                  fontWeight: 'bold',
                },
                enabled: true,
                borderWidth: 0,
                verticalAlign: 'bottom',
                formatter: function(){
                  var label = "<span>" + valorCumplimiento + '%' + '</span>';
                  return label;
                }
            }
        }
    },
    credits: {
        enabled: false
    },
    tooltip: {
        shared: true,
        hideDelay: 100,
        followPointer: true,
        pointFormat: '<b>{point.name}: {point.y:.2f}</b>%'
    },

    series: series
  }

  gauge.series[0].data[0].color = "grey";

  return gauge;
}