import { colors } from '../../highcharts/main';

function intToString (value) {
    var suffixes = ["", "k", "k", "b","t"];
    var suffixNum = Math.floor((""+value).length/3);
    var shortValue = parseFloat((suffixNum != 0 ? (value / 1000) : value).toPrecision(2));
    if (shortValue % 1 != 0) {
        var shortNum = shortValue.toFixed(1);
    }
    return shortValue+suffixes[suffixNum];
}

export function streetLightGauge(value, metaAnual){
  var color; var margen = metaAnual*0.90;
  if(value >=0 && value <margen){
    color = colors.c;
  }
  if(value >=margen && value <metaAnual){
    color = colors.y;
  }
  if(value >= metaAnual){
    color = colors.m;
  }
  return color;
}   

function percentOfMeta(metaAnual, cumplimiento){
    var result = cumplimiento / metaAnual * 100; 
    return result;
}

export function C4_makeGauge_data(title, measure, size, metaAnual, cumplimiento){

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

    var porcentajeDeMeta = percentOfMeta(metaAnual, cumplimiento);
  
    var gauge = {
      chart: {
        type: 'solidgauge',
        backgroundColor: 'transparent',
        height: size
      },
      title: {
        text: title+' '+measure,
        style: {
          fontFamily: 'Open Sans',
          color: 'white',
          fontSize:'2vh'
        }
      },
      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,
        tickPositions: [0, 100], // Posiciones para el inicial y meta anual
        tickWidth: 0,
        labels: {
            enabled: 'true',
            y: 24,
            style: {
                color: 'white',
                fontSize: '1.5vh',
                fontWeight: 'bold',                
            }
        },
        minPadding:1,
        zIndex: -1
      }, ],
      plotOptions: {
          solidgauge: {
              dataLabels: {
                  style: {
                    color: 'white',
                    fontSize: '2vh',
                    fontWeight: 'bold',
                    textOutline: false
                  },
                  enabled: true,
                  borderWidth: 0,
                  verticalAlign: 'bottom',
                  formatter: function(){
                    var cumplimientoSemanal = this.series.data[1].y;
                    var label = "<span>" + cumplimientoSemanal.toLocaleString() + '%</span>';
                    return label;
                  }
              }
          }
      },
      credits: {
          enabled: false
      },
      tooltip: {
            enabled: false,
            shared: true,
            hideDelay: 100,
            followPointer: true,

            pointFormatter: function(){
                return '<b>'+Math.round(porcentajeDeMeta,2)+'% de Cumplimiento'+'</b>';
            }
      },
  
      series: [{
  
        data: [{
  
          name: 'metaAnual',
          y: metaAnual,
          color: colors.lg
  
        }, {
  
          name: 'Cumplimiento',
          radius: 100,
          innerRadius: 60,
          y: cumplimiento,
          color: streetLightGauge(cumplimiento, metaAnual),
  
        }]
      }]
    }
    return gauge;
  }