package test.com.fp.common;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import com.fp.common.messages.MessageUtil;
import com.fp.dto.save.SaveRequest;

public class MessageTest {
    private MessageUtil mu;

    public MessageTest() throws Exception {
        SaveRequest sr = new SaveRequest();
        sr.setUser("GF");
        sr.setTransactionCode(150);
        sr.put("OfficeName", "Prueba");
        sr.put("val", new BigDecimal("129983.89"));
        sr.put("per", new BigDecimal("83.89"));
        sr.put("date", new Date(1323961406815L));
        this.mu = new MessageUtil("es");
        this.mu.setValue("name", "German Fiallos");
        this.mu.setValue("request", sr);
    }

    @Test
    public void name() throws Exception {
        this.mu.setPattern("$name");
        Assert.assertEquals("Nombre", "German Fiallos", this.mu.getMessage());
    }

    @Test
    public void user() throws Exception {
        this.mu.setPattern("$request.user");
        Assert.assertEquals("Usuario", "GF", this.mu.getMessage());
    }

    @Test
    public void transaction() throws Exception {
        this.mu.setPattern("$request.transactionCode");
        Assert.assertEquals("Transaccion", "150", this.mu.getMessage());
    }

    @Test
    public void date() throws Exception {
        this.mu.setPattern("$date.format('yyyy-MM-dd', $request.date)");
        Assert.assertEquals("Date", "2011-12-15", this.mu.getMessage());
        this.mu.setPattern("$date.format('yyyy-MMM-dd', $request.date,$locate)");
        Assert.assertEquals("Date", "2011-dic-15", this.mu.getMessage());
        this.mu.setPattern("$date.format(\"EEEEEEEEEEEEEEEEE, d 'de' MMMMMMMMMM 'de' yyyy\", $request.date,$locate)");
        Assert.assertEquals("Date", "jueves, 15 de diciembre de 2011", this.mu.getMessage());

    }

    @Test
    public void amount() throws Exception {
        this.mu.setPattern("$number.currency($request.val)");
        Assert.assertEquals("Monto", "$129,983.89", this.mu.getMessage());
        this.mu.setPattern("$number.format($request.val)");
        Assert.assertEquals("Monto", "129,983.89", this.mu.getMessage());
    }

    @Test
    public void cond() throws Exception {
        this.mu.setPattern("#if( $cond ) OK #else NO #end");
        this.mu.setValue("cond", Boolean.TRUE);
        Assert.assertEquals("Date", " OK ", this.mu.getMessage());
        this.mu.setValue("cond", Boolean.FALSE);
        Assert.assertEquals("Date", " NO ", this.mu.getMessage());
    }

    @Test
    public void loop() throws Exception {
        this.mu.setPattern("#foreach( $val in $list ) $val.code $val.value #end");
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 3; i++) {
            Map<String, Object> rec = new HashMap<String, Object>();
            rec.put("code", i);
            rec.put("value", "Prueba-" + i);
            data.add(rec);
        }
        this.mu.setValue("list", data);
        Assert.assertEquals("Date", " 0 Prueba-0  1 Prueba-1  2 Prueba-2 ", this.mu.getMessage());
    }

}
