package org.rpwf.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.rpwf.model.ModelClass;
import org.rpwf.util.ControllerHelper.RangeObj;

public class ControllerHelperTest {
  
  @Test
  public void getConstrainAnnotationsTest() {
    Class<? extends ModelClass> t = TestModel.class;
    
    Map<String, Object> testMap = ControllerHelper.getConstrainAnnotations(t);
    
    RangeObj r1 = (RangeObj) testMap.get("rightLong");
    assertTrue((long)r1.getMin() == 0);
    assertTrue((long)r1.getMax() == 10);
    
    RangeObj r2 = (RangeObj) testMap.get("falseLong");
    assertTrue(r2 == null);
    
    RangeObj r3 = (RangeObj) testMap.get("rightInt");
    assertTrue((int)r3.getMin() == 0);
    assertTrue((int)r3.getMax() == 10);
    
    RangeObj r4 = (RangeObj) testMap.get("IntWithoutMax");
    assertTrue((int)r4.getMax() == Integer.MAX_VALUE);
    
    RangeObj r5 = (RangeObj) testMap.get("IntWithoutMin");
    assertTrue((int)r5.getMin() == Integer.MIN_VALUE);
    
    String r6 = (String) testMap.get("rightString");
    assertTrue(r6.equals("%"));
    
    RangeObj r7 = (RangeObj) testMap.get("rightDate");
    SimpleDateFormat testFormat = new SimpleDateFormat("dd.MM.yyyy");
    if(r7 == null){
      System.out.println("t");
    }
    try {
      assertTrue(((Date) r7.getMin()).getDay() ==  testFormat.parse("01.01.1990").getDay() &&
	  ((Date) r7.getMin()).getMonth() ==  testFormat.parse("01.01.1990").getMonth() &&
	  ((Date) r7.getMin()).getYear() ==  testFormat.parse("01.01.1990").getYear());
      assertTrue(((Date) r7.getMax()).getDay() ==  testFormat.parse("02.02.1990").getDay() &&
	  ((Date) r7.getMax()).getMonth() ==  testFormat.parse("02.02.1990").getMonth() &&
	  ((Date) r7.getMax()).getYear() ==  testFormat.parse("02.02.1990").getYear());
    } catch (ParseException e) {
      e.printStackTrace();
    }
    
    Date r8 = (Date) testMap.get("falseDate");
    assertTrue(r8 == null);

    //TODO Double, Float, w/o min, w/o max
    
    RangeObj r9 = (RangeObj) testMap.get("rightFloat");
    assertTrue((float)r9.getMin() == 0.5);
    assertTrue((float)r9.getMax() == 0.75);
    
    RangeObj r10 = (RangeObj) testMap.get("FloatWithoutMin");
    assertTrue((float)r10.getMin() == Float.MIN_VALUE);
    
    RangeObj r11 = (RangeObj) testMap.get("FloatWithoutMax");
    assertTrue((float)r11.getMax() == Float.MAX_VALUE);
    
    RangeObj r12 = (RangeObj) testMap.get("rightDouble");
    assertTrue((double)r12.getMin() == 0.5);
    assertTrue((double)r12.getMax() == 0.75);
    
    RangeObj r13 = (RangeObj) testMap.get("DoubleWithoutMin");
    assertTrue((double)r13.getMin() == Double.MIN_VALUE);
    
    RangeObj r14 = (RangeObj) testMap.get("DoubleWithoutMax");
    assertTrue((double)r14.getMax() == Double.MAX_VALUE);
  }
  
  @Test
  public void getAnnotatedTitles(){
    Class<? extends ModelClass> t = TestModel.class;
    
    Map<String, String> testMap = ControllerHelper.getAnnotatedTitles(t);
    
    assertEquals("ID", testMap.get("rpwfID"));
    assertEquals(null, testMap.get("rightLong"));
  }
  
  @Test
  public void getRequiredAnnotations(){
    Class<? extends ModelClass> t = TestModel.class;
    
    List<String> testMap = ControllerHelper.getRequiredAnnotations(t);
    
    assertTrue(testMap.contains("rpwfID"));
    assertFalse(testMap.contains("rightLong"));
    assertTrue(testMap.size()==1);
    
  }
  
}
