package com.fitbank.arreglos.formulario;

import com.fitbank.arreglos.ArregloFormulario;
import com.FitBank.xml.Formas.AreaTexto;
import com.FitBank.xml.Formas.Boton;
import com.FitBank.xml.Formas.Combo;
import com.FitBank.xml.Formas.Datos;
import com.FitBank.xml.Formas.Formulario;
import com.fitbank.css.FontWidths;
import com.fitbank.css.HojaDeEstilos;
import com.fitbank.css.ParserCSS;
import java.util.Iterator;
import org.apache.commons.lang.StringUtils;

/**
 * Convierte el ancho de los elementos a pixeles.
 *
 * @author FitBank CI
 */
public class ArregloAnchoDatos implements ArregloFormulario {

    public static HojaDeEstilos hojaDeEstilos;

    static {
        hojaDeEstilos = ParserCSS.parse(Thread.currentThread().
                getContextClassLoader().
                getResource("com/fitbank/web/css/estilo.css"));
    }

    public boolean arreglar(Formulario formulario) {
        boolean arreglado = false;

        Iterator<Datos> i = formulario.iterator(Datos.class);
        while (i.hasNext()) {
            Datos datos = i.next();
            if (datos.getClass().equals(Datos.class)
                    || datos.getClass().equals(AreaTexto.class)) {
                arreglado |= arreglarAncho(datos);
            }
        }

        return arreglado;
    }

    public static boolean arreglarAncho(Datos datos) {
        String fontSizeString = hojaDeEstilos.getEstilo(datos.getEstilo()).
                getPropiedad("font-size");
        int fontSize = StringUtils.isEmpty(fontSizeString) ? 11 : Integer.
                parseInt(fontSizeString.replaceAll("px", ""));

        String fontFamilyString = hojaDeEstilos.getEstilo(datos.getEstilo()).
                getPropiedad("font-family");
        String fontFamily = StringUtils.isEmpty(fontFamilyString) ? "Verdana"
                : fontFamilyString;

        int ancho = Integer.parseInt(datos.getAncho());
        int w = datos.getW();
        int h = datos.getH();

        // Ver clase Datos#toHtml. Gana ancho siempre a menos que sea 0.
        if (ancho != 0) {
            datos.setW(0);
        }

        if (ancho == 0 && datos.getW() == 0) {
            if (datos.getClass().equals(Boton.class) || datos.getClass().equals(
                    Combo.class)) {
                Boton boton = (Boton) datos;
                ancho = boton.getDatos().length();
            } else {
                ancho = 20;
            }

        }

        if (datos.getW() == 0) {
            datos.setW(FontWidths.getWidth(fontFamily, fontSize, ancho));
            datos.setAncho("");
        }

        boolean cambio = false;
        if (datos.getClass().equals(AreaTexto.class)) {
            AreaTexto areaTexto = (AreaTexto) datos;

            int alto = Integer.parseInt(areaTexto.getAlto());

            // Adaptado de la clase AreaTexto
            if (alto == 1 && areaTexto.getH() != 0) {
                areaTexto.setH(areaTexto.getH() > 0 ? areaTexto.getH() : 0);
            } else if (alto == 2 && areaTexto.getH() == 0) {
                areaTexto.setH(25);
            } else if (alto > 2 && areaTexto.getH() == 0) {
                datos.setH(fontSize * alto + 2);
            } else {
                areaTexto.setH(25);
            }

            if (!areaTexto.getAlto().equals("0")) {
                areaTexto.setAlto("0");
                cambio = true;
            }
        }

        return !String.valueOf(ancho).equals(datos.getAncho()) || w != datos.
                getW() || h != datos.getH() || cambio;
    }

}
