package com.cyosp.mpa.api.rest.homebank.v1dot2.model;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by CYOSP on 2017-07-25.
 */
@Getter
@Setter
public class Operation {

    @XStreamOmitField
    private Integer key;

    //-----------------------------

    @XStreamAsAttribute
    @XStreamAlias("date")
    private Long date;

    @XStreamAsAttribute
    @XStreamAlias("amount")
    private BigDecimal amount;

    @XStreamAsAttribute
    @XStreamAlias("account")
    private Integer account;

    @XStreamAsAttribute
    @XStreamAlias("paymode")
    private Integer paymode;

    @XStreamAsAttribute
    @XStreamAlias("flags")
    private Integer flags;

    @XStreamAsAttribute
    @XStreamAlias("payee")
    private Integer payee;

    @XStreamAsAttribute
    @XStreamAlias("wording")
    private String wording;

    @XStreamAsAttribute
    @XStreamAlias("category")
    private Integer category;

    //--------------------------

    @XStreamOmitField
    private Date javaDate;

    @XStreamOmitField
    private String payeeName;

    @XStreamOmitField
    private String categoryName;

    @XStreamOmitField
    private Currency currency;

    @XStreamOmitField
    private BigDecimal balance;

    public void convertDateToJulian() {

        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(getJavaDate());

        Calendar calendar = new GregorianCalendar(1, GregorianCalendar.JANUARY, 1);
        long diffInMs = gregorianCalendar.getTimeInMillis() - calendar.getTimeInMillis();

        setDate(diffInMs / (24 * 60 * 60 * 1000) - 1);
    }

    public void convertJulianToDate() {

        long diffInMs = getDate() * (24 * 60 * 60 * 1000);

        GregorianCalendar gregorianCalendar = new GregorianCalendar(1, GregorianCalendar.JANUARY, 1);
        long dateInMs = diffInMs + gregorianCalendar.getTimeInMillis();
        int dayNbr = (int) (dateInMs / (24 * 60 * 60 * 1000));

        GregorianCalendar newGregorianCalendar = new GregorianCalendar();
        newGregorianCalendar.setTimeInMillis(0);
        newGregorianCalendar.add(GregorianCalendar.DAY_OF_MONTH, dayNbr + 2);

        setJavaDate(newGregorianCalendar.getTime());
    }
}

@@ -1108,23 +1108,24 @@
 mAlias(%22
-wording
+category
 %22)%0A    p
@@ -1135,22 +1135,24 @@
 ate 
-String wording
+Integer category
 ;%0A%0A 
@@ -1189,32 +1189,31 @@
 treamAlias(%22
-category
+wording
 %22)%0A    priva
@@ -1215,32 +1215,30 @@
 private 
-Integer category
+String wording
 ;%0A%0A    /

Reorder in order to have same XML output order as HomeBank