package com.bloatit.framework;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.Locale;

import com.bloatit.model.data.DaoComment;
import com.bloatit.model.data.DaoDemand;
import com.bloatit.model.data.DaoDescription;
import com.bloatit.model.data.DaoExternalAccount;
import com.bloatit.model.data.DaoExternalAccount.AccountType;
import com.bloatit.model.data.DaoGroup;
import com.bloatit.model.data.DaoMember;
import com.bloatit.model.data.DaoTransaction;
import com.bloatit.model.data.util.SessionManager;
import com.bloatit.model.exceptions.NotEnoughMoneyException;

public class BigDB {

    private static final int nbUsers = 100;

    public BigDB() {

        SessionManager.beginWorkUnit();

        // Create some users
        for (int i = 0; i < nbUsers; i++) {
            DaoMember member;
            member = DaoMember.createAndPersist("member " + i, new Integer(i).toString(), "mail@nowhere.com");
            member.setFullname("User " + i + " Fullname");
            member.setExternalAccount(DaoExternalAccount.createAndPersist(member, AccountType.IBAN, "code"));
            try {
                DaoTransaction
                        .createAndPersist(member.getInternalAccount(), member.getExternalAccount(), new BigDecimal("-1000"));
            } catch (final NotEnoughMoneyException e) {
                e.printStackTrace();
            }
            if (i % 500 == 0) {
                SessionManager.flush();
                SessionManager.clear();
            }
        }

        // Create Some Groups
        for (int i = 0; i < nbUsers / 4; i += 4) {
            final DaoGroup group = DaoGroup.createAndPersiste("group " + (i / 4), "plop@plop.com", DaoGroup.Right.PUBLIC);
            group.addMember(DaoMember.getByLogin("member " + i), true);
            group.addMember(DaoMember.getByLogin("member " + (i + 1)), false);
            group.addMember(DaoMember.getByLogin("member " + (i + 2)), false);
            group.addMember(DaoMember.getByLogin("member " + (i + 3)), false);
            if (i % 500 == 0) {
                SessionManager.flush();
                SessionManager.clear();
            }
        }
        for (int i = 0; i < nbUsers; i++) {
            final DaoDemand demand1 = DaoDemand.createAndPersist(DaoMember.getByLogin("member " + i), new DaoDescription(
                    DaoMember.getByLogin("member " + i), new Locale("fr"), fortune(140), fortune() + fortune() + fortune()));
            DaoDemand.createAndPersist(DaoMember.getByLogin("member " + i),
                    new DaoDescription(DaoMember.getByLogin("member " + i), new Locale("fr"), fortune(140), fortune() + fortune()
                            + fortune() + fortune()));

            int commentCount = (int) (Math.random() * 5);

            for (int j = 0; j < commentCount; j++) {

                final DaoComment comment = DaoComment.createAndPersist(DaoMember.getByLogin("member " + i), fortune());
                createComment(comment);
                demand1.addComment(comment);
            }

            // TODO add Transaction
            // TODO add Translation
            // TODO add Contributions
            // TODO add Offers

            if (i % 100 == 0) {
                SessionManager.flush();
                SessionManager.clear();
            }
        }

        SessionManager.endWorkUnitAndFlush();

    }

    private void createComment(DaoComment comment) {
        final int nbComment = (int) (Math.random() * 15);
        for (int i = 0; i < nbComment; i++) {
            final int memberNum = (int) (Math.random() * nbUsers);
            final DaoComment other = DaoComment.createAndPersist(DaoMember.getByLogin("member " + memberNum), fortune());
            comment.addChildComment(other);
            if ((int) (Math.random() * 15) % 15 == 0) {
                createComment(other);
            }
        }
    }

    public static void main(String[] args) {
        new BigDB();
    }

    public static String fortune() {
        String text = "";
        try {
            String line;
            Process p = Runtime.getRuntime().exec("fortune");
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = input.readLine()) != null) {
                text += line;
            }
            input.close();
        } catch (Exception err) {
            err.printStackTrace();
        }

        if (text.isEmpty()) {
            System.err.println("please install fortune for more fun (sudo apt-get install fortune-mod)");
            text = Long.toHexString(Double.doubleToLongBits(Math.random()));
        }

        return text;
    }

    private String fortune(int i) {
        String text = fortune();
        if (text.length() > i) {
            text = text.substring(0, i);
        }

        return text;
    }
}

@@ -119,16 +119,67 @@
 ecimal;%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -836,16 +836,78 @@
 e users%0A
+        List%3CDaoMember%3E members = new ArrayList%3CDaoMember%3E();%0A
         
@@ -1248,24 +1248,70 @@
 , %22code%22));%0A
+            members.add(member);%0A            %0A
             
@@ -1953,41 +1953,20 @@
 ber(
-DaoM
+m
 ember
+s
 .get
-ByLogin(%22member %22 + 
+(
 i), 
@@ -2004,49 +2004,26 @@
 ber(
-DaoM
+m
 ember
+s
 .get
-ByLogin(%22member %22 + 
 (i + 1)
-)
 , fa
@@ -2060,49 +2060,26 @@
 ber(
-DaoM
+m
 ember
+s
 .get
-ByLogin(%22member %22 + 
 (i + 2)
-)
 , fa
@@ -2116,49 +2116,26 @@
 ber(
-DaoM
+m
 ember
+s
 .get
-ByLogin(%22member %22 + 
 (i + 3)
-)
 , fa
@@ -2389,41 +2389,20 @@
 ist(
-DaoM
+m
 ember
+s
 .get
-ByLogin(%22member %22 + 
+(
 i), 
@@ -2445,41 +2445,20 @@
     
-DaoM
+m
 ember
+s
 .get
-ByLogin(%22member %22 + 
+(
 i), 
@@ -2569,41 +2569,20 @@
 ist(
-DaoM
+m
 ember
+s
 .get
-ByLogin(%22member %22 + 
+(
 i),%0A
@@ -2624,41 +2624,20 @@
 ion(
-DaoM
+m
 ember
+s
 .get
-ByLogin(%22member %22 + 
+(
 i), 
@@ -2934,41 +2934,20 @@
 ist(
-DaoM
+m
 ember
+s
 .get
-ByLogin(%22member %22 + 
+(
 i), 

store member in an array to optimize db call