import java.io.*;
import java.net.URL;
import java.sql.Timestamp;

/**
 * Created by IntelliJ IDEA.
 * User: sagarmichael
 * Date: 13/03/2012
 * Time: 13:53
 * To change this template use File | Settings | File Templates.
 */
public class OrderLog {

    private static PrintWriter out;

      public static void orderLog(String url){
                     System.out.println("OrderLog started");
          try{
                    //File file2 = new File("/Users/sagarmichael/Desktop/tmp2.html");
                    //file2.delete();
                    //File oldFile = new File("/Users/sagarmichael/Desktop/dlog.html");
                    //oldFile.renameTo(new File("/Users/sagarmichael/Desktop/dtmp.html"));
                    File oldFile = new File("/home/backuppc/made4u-nagios/olog.html");
                    oldFile.renameTo(new File("/home/backuppc/made4u-nagios/otmp.html"));

                    //BufferedReader in = new BufferedReader(new FileReader("/Users/sagarmichael/Desktop/dtmp.html")) ;
                    BufferedReader in = new BufferedReader(new FileReader("/home/backuppc/made4u-nagios/otmp.html")) ;

                    //FileWriter outFile = new FileWriter("/Users/sagarmichael/Desktop/dlog.html");
                    FileWriter outFile = new FileWriter("/home/backuppc/made4u-nagios/olog.html");
                    out = new PrintWriter(outFile);

                    String str;
                    Boolean first = true;
                    while ((str = in.readLine()) != null){

                        if(first && str.contains("</tr>")){
                            out.println(str);
                            parser(url);


                            first = false;
                        }
                        else{ out.println(str); }

                    }
                    out.close();
                }catch (Exception e) {
                    System.out.println("file write Error: " + e.getMessage());
                }
            }

            public static void parser(String url2)throws Exception{
                URL url =null;
                url = new URL("http://made4u.k-int.com/automaticDesign/design/list/list?offset=0&max=100&sort=requestSubmittedTime&order=desc");
                // String name = url2.replace("http://made4u.k-int.com/automaticDesign/","");
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                String str;
                int i = 0;
                int col = 0;
                int row = 0;
                String[][] strA = new String[4][100];
                while ((str = in.readLine()) != null) {
                    if(str.contains("ID")){
                        System.out.println("str: " + str);
                        String tmp[] = str.split("\">");
                        tmp = tmp[1].split("</");
                        strA[0][0] = tmp[0];
                        System.out.println("strA: " + strA[0][0]);
                    }
                    else  if(str.contains("Order ID")){
                        System.out.println("str: " + str);
                        String tmp[] = str.split("\">");
                        tmp = tmp[1].split("</");
                        strA[1][0] = tmp[0];
                        System.out.println("strA: " + strA[1][0]);
                    }
                    else  if(str.contains("\">Status")){
                        System.out.println("str: " + str);
                        String tmp[] = str.split("\">");
                        tmp = tmp[1].split("</");
                        strA[2][0] = tmp[0];
                        System.out.println("strA: " + strA[2][0]);
                    }
                    else  if(str.contains("\">Request time<")){
                        System.out.println("str: " + str);
                        String tmp[] = str.split("\">");
                        tmp = tmp[1].split("</");
                        strA[3][0] = tmp[0];
                        System.out.println("strA: " + strA[3][0]);
                    }
                    else{
                        if(str.contains("<a href=\"/automaticDesign/design/") && str.contains("/show\">")){
                            //                          System.out.println("str: " + str);
                            String tmp[] = str.split("\">");
                            tmp = tmp[1].split("</");
                            strA[i][row] = tmp[0];
                            System.out.println("strA["+i+"]["+row+"]" + strA[i][row]);

                            i++;
                            if(i == 4){
                                i =0 ;
                                row++;
                            }
                        }
                    }
                }



                Boolean reported = alreadyReported(strA,url2);
                System.out.println("Reported? "+reported);
                System.out.println("Url2: "+url2);
                if(reported){
                    if(url2.contains("Error") && reported){
                        String tmp[] = url2.split("/show");
                        tmp =  tmp[0].split("/design/");
                        int id = Integer.parseInt(tmp[1]);
                         System.out.println("id: "+id);
                          // System.out.println(row);
                            int row2 =0;
                        while(row2 <=100 ){
                            System.out.println("while " + row2);
                            System.out.println("strA"+"["+row2+"]"+"[1] : " + strA[0][row2]);
                            if(strA[0][row2] == null){
                                System.out.println("break!");
                                break;
                            }
                            if(strA[0][row2].contains(tmp[1])){
                                          out.println("</tr>\n" +
                                          "<tr>\n" +
                                          "<th class=\"spec\" abbr=\"Model\" scope=\"row\">Error-"+getTime()+"</th>\n" +
                                          "<td>"+strA[0][row2]+"</td>\n" +
                                          "<td>"+strA[1][row2]+"</td>\n" +
                                          "<td>"+strA[2][row2]+"</td>\n" +
                                          "<td>"+strA[3][row2]+"</td>\n" +
                                          "</tr>");
                                System.out.println("</tr>\n" +
                                        "<tr>\n" +
                                        "<th class=\"spec\" abbr=\"Model\" scope=\"row\">Error-"+getTime()+"</th>\n" +
                                        "<td>"+strA[1][row2]+"</td>\n" +
                                        "<td>"+strA[2][row2]+"</td>\n" +
                                        "<td>"+strA[3][row2]+"</td>\n" +
                                        "</tr>");
                                      System.out.println("break!");
                                break;
                            }
                            row2++;
                        }
                    }
                }
            }


            public static String getTime(){
                java.util.Date date= new java.util.Date();

                return(new Timestamp(date.getTime()).toString());

            }

            public static boolean alreadyReported(String[][] strA, String url2) throws Exception {


                URL url = new URL("http://www.nagios-made4u.com/olog.html");

                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                //  BufferedReader in = new BufferedReader(new FileReader("/Users/sagarmichael/Desktop/log.html"));


                String str;
                String sline = "";
                int i2 = 0;
                int i = 1;
                System.out.println("--------------------------------------------read line: " + in.readLine());
                while ((str = in.readLine()) != null) {
                    System.out.println("STR: " + str);
                    if(str.contains("<td>")){
                        System.out.println("BOOOM");
                        System.out.println("STR: " + str);
                        i2++ ;
                        System.out.println(i2);
                        sline = sline + str;

                        if(i2==4){

                            i2 =0;

                           while(i < 100){
                       // System.out.println("strA: "+strA[3][i]+ "   sline : "+ sline);
                        if(sline.contains(strA[3][i])){
                        }
                        if(sline.contains(strA[3][i]) && url2.contains(strA[3][i])){
                            return false;

                        }

                          i++;
                    }

                    sline = "";
                    i = 0;
                }
                        System.out.println("i: " + i);

                    }

                }

                return true;
            }
        }
