/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arrebol.taxiservicios.beans.report;

import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.view.report.DailySaleController;
import com.arrebol.taxiservicios.model.view.report.DailySale;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Picasso
 */
@Named
@ViewScoped
public class DailySaleBean extends TaxiGenericBean implements Serializable {

    /**
     *
     * @param split
     * @param notes
     * @return
     */
    private String distinctData(String[] split, boolean notes) {
        StringBuilder builder = new StringBuilder();

        for (String value : split) {
            if (notes) {
                builder.append(value);
            } else {
                builder.append(translateMonth(value));
            }
            builder.append(",");
        }
        
        int length = builder.toString().length();
        
        return builder.substring(0, length - 1);
    }

    private String translateMonth(String englishDate) {
        String output = "";

        for (int i = 0; i < englishMonths.size(); i++) {
            if (englishDate.contains(englishMonths.get(i))) {
                output = englishDate.replace(englishMonths.get(i), spanishMonths.get(i));
                break;
            }
        }

        return output;
    }

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            setController(new DailySaleController());
            setDailySales(getController().findAllDailySalesByDate());

            getDailySales().forEach(ds -> {
                String[] split = null;

                if (!ds.getPayDay().equals("No registro venta hoy")) {
                    split = ds.getPayDay().split(",");

                    if (split != null && split.length > 1) {
                        ds.setPayDay(distinctData(split, false));
                    } else {
                        ds.setPayDay(translateMonth(ds.getPayDay()));
                    }
                }

                if (!ds.getPayPending().equals("Al corriente")) {
                    split = ds.getPayPending().split(",");

                    if (split != null && split.length > 1) {
                        ds.setPayPending(distinctData(split, false));
                    } else {
                        ds.setPayPending(translateMonth(ds.getPayPending()));
                    }
                }

                if (null != ds.getNotes()) {
                    split = ds.getNotes().split(",");

                    if (split != null && split.length > 1) {
                        ds.setNotes(distinctData(split, true));
                    }
                }
            });
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    private static final long serialVersionUID = 3542890067257063118L;

    private DailySaleController controller;
    private List<DailySale> dailySales;
    private final List<String> spanishMonths = Arrays.asList("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
    private final List<String> englishMonths = Arrays.asList("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

    final Logger logger = LogManager.getLogger(DailySaleBean.class);

    public DailySaleBean() {

    }

    public DailySaleController getController() {
        return controller;
    }

    public void setController(DailySaleController controller) {
        this.controller = controller;
    }

    public List<DailySale> getDailySales() {
        return dailySales;
    }

    public void setDailySales(List<DailySale> dailySales) {
        this.dailySales = dailySales;
    }

}
