import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;

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

/**
 * Created by IntelliJ IDEA.
 * User: sagarmichael
 * Date: 05/03/2012
 * Time: 10:34
 * To change this template use File | Settings | File Templates.
 */
public class TimerLog {

    private static PrintWriter out;

    public static void htmlParser(String url){

        try{
            //File file2 = new File("/Users/sagarmichael/Desktop/tmp2.html");
            //file2.delete();
            //File oldFile = new File("/Users/sagarmichael/Desktop/log.html");
            //oldFile.renameTo(new File("/Users/sagarmichael/Desktop/tmp.html"));
            File oldFile = new File("/home/backuppc/made4u-nagios/log.html");
            oldFile.renameTo(new File("/home/backuppc/made4u-nagios/tmp.html"));

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

            //FileWriter outFile = new FileWriter("/Users/sagarmichael/Desktop/log.html");
            FileWriter outFile = new FileWriter("/home/backuppc/made4u-nagios/log.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("Error: " + e.getMessage());
        }
    }

    public static void parser(String url2)throws Exception{
        URL url =null;
        url = new URL("http://made4u.k-int.com/automaticDesign/timerData/list");
        // 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 = 1;
        String[][] strA = new String[4][7];
        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("Timer type")){
                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("\">Last run 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/timerData/") && 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 = true;
                reported = alreadyReported(strA,url2);
        System.out.println("Url2: "+url2);
        if(reported){
        if(url2.contains("Manufacturing order frame calculation progress")){
            out.println("</tr>\n" +
                    "<tr>\n" +
                    "<th class=\"spec\" abbr=\"Model\" scope=\"row\">Error-"+getTime()+"</th>\n" +
                    "<td>"+strA[1][3]+"</td>\n" +
                    "<td>"+strA[3][3]+"</td>\n" +
                    "</tr>");

        }
        else if(url2.contains("Manufacturing order part 2")){
            out.println("</tr>\n" +
                    "<tr>\n" +
                    "<th class=\"spec\" abbr=\"Model\" scope=\"row\">Error-"+getTime()+"</th>\n" +
                    "<td>"+strA[1][4]+"</td>\n" +
                    "<td>"+strA[3][4]+"</td>\n" +
                    "</tr>");

        }
        else if(url2.contains("Manufacturing order")){
            out.println("</tr>\n" +
                    "<tr>\n" +
                    "<th class=\"spec\" abbr=\"Model\" scope=\"row\">Error-"+getTime()+"</th>\n" +
                    "<td>"+strA[1][1]+"</td>\n" +
                    "<td>"+strA[3][1]+"</td>\n" +
                    "</tr>");

        }
        else if(url2.contains("Design request part 2")){

            System.out.println("Design request part 2");
            out.println("</tr>\n" +
                    "<tr>\n" +
                    "<th class=\"spec\" abbr=\"Model\" scope=\"row\">Error-"+getTime()+"</th>\n" +
                    "<td>"+strA[1][5]+"</td>\n" +
                    "<td>"+strA[3][5]+"</td>\n" +
                    "</tr>");

        }
        else if(url2.contains("Design request")){
            out.println("</tr>\n" +
                    "<tr>\n" +
                    "<th class=\"spec\" abbr=\"Model\" scope=\"row\">Error-"+getTime()+"</th>\n" +
                    "<td>"+strA[1][2]+"</td>\n" +
                    "<td>"+strA[3][2]+"</td>\n" +
                    "</tr>");

        }



        System.out.println("here!!!!l!");

    }
    }

    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/log.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>")){

                    i2++ ;
                    System.out.println(i2);
                    sline = sline + str;

                    if(i2==2){

                        i2 =0;

                        while(i < 6){
                       // 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;
    }

}
