/*
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation.

 *  There is no copyright.

 *  MockwareTeam (C) 2009

 */

package core;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;

/**
 *
 * @author yetkin
 */
public class Reason extends util{

    static boolean start;
    static String [] fields;
    static Vector <String> tmp = new Vector <String>();
    static Vector <String> mines= new Vector <String>();
    static ResultSet theResult;
    static String main_query;
    static String  curr;
    static Connection connr = db.dbConnect("jdbc:mysql://localhost:3306/Mockware", "root", "qropadb");
    /*public static void main(String args[]){
    String a = "112";
    int d = Integer.parseInt(a);
    int f = 2+ d;
    System.out.println("f"+f);
    String hhh="dfsdf!!asdasd";
    String [] jj = hhh.split("!!");
    int i;
    for(i=0;i<jj.length;i++)
    System.out.println("m"+jj[i]+"y");
    try {
    Statement hh = connr.createStatement();
    hh.execute("Create Database fart");System.out.println("maka"+"y");*/
            /*hh.execute("CREATE TABLE custome (First_Name char(50),Last_Name char(50)," +
            "Address char(50) default 'Unknown'," +
            "City char(50) default 'Mumbai'," +
            "Country char(25)," +
            "Birth_Date date)");*/

      /*} catch (SQLException ex) {
            Logger.getLogger(Reason.class.getName()).log(Level.SEVERE, null, ex);
        }

    if(jj[0].isEmpty()) System.out.println("bos");
    Taken("");
    for(i=0;i<mines.size();i++)
    System.out.println(mines.elementAt(i));

    }*/

    private static void divor(String string) {
        throw new UnsupportedOperationException("Not yet implemented");
    }


    private static String erkan() {
        throw new UnsupportedOperationException("Not yet implemented");
    }



    public Reason(){}
    public static Vector <String> Taken(String t)
    {
        //main_query=erkan();
        String [] h,f;
        main_query=t;//"Select Name From askerlik Where Soldier='NO'";
        Vector <ResultSet> temp;
        h = main_query.split("From");
        fields= h[0].split("Select")[1].split(",");
        mines = new Vector <String> ();

        //System.out.println("ee"+h[1]+"bb");
        //System.out.println("gg"+h+"jj");
        int i;
        String a;
        DB my =new DB();
        
        //Connection connf = db.dbConnect("jdbc:mysql://localhost:3306/Mockware_Factset", "root", "qropadb");
        try {

            Statement stmt = connr.createStatement();
            //Statement stt = connr.createStatement();
            ResultSet rs = stmt.executeQuery("Select * From" +h[1]);
            System.out.println("papa"+rs.toString()+"mama");
            // rs.afterLast();
            //ResultSet rst = stt.executeQuery("Select * From askerlik Where Rule_Id=3" );
            //rst.beforeFirst();
            //rs=rst;
            //ResultSetMetaData rsmd = rs.getMetaData();
            //while(rs.next()) System.out.println(rs.getString(1));
            start=false;
            while(rs.next())
            { 
                a=rs.getString(2);
                System.out.println(a+"uu");
                if(a.equalsIgnoreCase("simple"))
                {
                    for(i=3;;i++)
                    {
                        if(rs.getString(i).equalsIgnoreCase("then"))
                        break;
                        if(!rs.getString(i).isEmpty())
                        curr=rs.getString(i);
                    }System.out.println(curr+"oo");
                    Forwrd(curr);
                }
                else
                {
                    Division(a);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(Reason.class.getName()).log(Level.SEVERE, null, ex);
        }
        return mines;
    }
    private static void Division(String a) {
        try {
            String[] divsor;
            String[] divsan;
            Statement stin = connr.createStatement();
            
            ResultSet rsin;
            //ResultSetMetaData mdin;
            int i,p;
            divsor = a.split("&&");
            System.out.println(divsor[1]+"tr"+divsor.length);
            for (i = 0; i < divsor.length; i++) 
            {System.out.println("papa"+"+"+"mama");
                if(divsor[i].contains("||"))
                    divor(divsor[i]);
                else 
                {
                    rsin=stin.executeQuery("Select * From askerlik Where Rule_Name='"+divsor[i]+"'");
                    //mdin=rsin.getMetaData();
                   
                    while(rsin.next())
                    { 
                        
                        a=rsin.getString(2);
                        if(a.equalsIgnoreCase("simple"))
                        {    
                            for(p=3;;p++)
                            {
                                if(rsin.getString(p).equalsIgnoreCase("then"))
                                break;
                                if(!rsin.getString(p).isEmpty())
                                curr=rsin.getString(p);

                            }
                            System.out.println("neki"+curr+"bu");
                            Forwrd(curr);
                        }
                        else
                        {
                            Division(a);
                        }    
                    }
                }System.out.println("papa"+"-"+"mama");
            }System.out.println("papa"+"*"+"mama");
            //divsan = a.split("||");
        } catch (SQLException ex) {
            Logger.getLogger(Reason.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    private static void Forward(byte[] a) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private static void Forwrd(String curr) {
        
        String [] fare;
        String exp;
        String fild;
        fare=curr.split("!!");

        if (fare[1].equalsIgnoreCase("int"))//internal fact
        {
            try {
                Connection connf = db.dbConnect("jdbc:mysql://localhost:3306/Mockware_Factset", "root", "qropadb");
                fild = fare[2];//field that we check the rule for
                exp = fare[3];//that will be compared with field
                fare = fild.split("_");System.out.println(fild);
                Statement myst = connf.createStatement();
                ResultSet rs = myst.executeQuery("Select " +fields[0]+ " From " + fare[0] + " Where " + fare[1]  + exp );
                int y=0;
                if(!tmp.isEmpty())
                tmp.clear();
                while(rs.next())
                {

                    tmp.add(rs.getString(1));
                    y++;
                }
                int l,o;
                y=0;
                Vector <String> min = new Vector <String>() ;
                
                if(start)
                {
                   
                    min.addAll(mines);
                    System.out.println(mines.toString());
                    System.out.println(min.toString());
                    System.out.println(tmp.toString());
                    mines.clear();
                    for(l=0;l<tmp.size();l++)
                    {
                        for(o=0;o<min.size();o++)
                        {
                            if(min.elementAt(o).equalsIgnoreCase(tmp.elementAt(l)))
                            {
                                mines.add(min.elementAt(o));
                                y++;
                            }
                        }
                    }
                }
                else {mines.addAll(tmp); start=true;}


            } catch (SQLException ex) {
                Logger.getLogger(Reason.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else if (fare[1].equalsIgnoreCase("web"))
        {
        }
        else if (fare[1].equalsIgnoreCase("app"))
        {}
        else if (fare[1].equalsIgnoreCase("ext"))
        {
            try {
                String[] jkjk;
                jkjk = fare[2].split("#"); //0 url 1 dbname 2 pswd
                DB hh = new DB();
                Connection connf = hh.exdbConnect(jkjk[0], jkjk[1], jkjk[2]);
                Statement myst = connf.createStatement();
                ResultSet rs = myst.executeQuery("Select " + fields[0] + " From " + jkjk[3] + " Where " + jkjk[4] + fare[3]);
                int y=0;
                if(!tmp.isEmpty())
                tmp.clear();
                while(rs.next())
                {

                    tmp.add(rs.getString(1));
                    y++;
                }
                int l,o;
                y=0;
                Vector <String> min = new Vector <String>() ;

                if(start)
                {

                    min.addAll(mines);
                    System.out.println(mines.toString());
                    System.out.println(min.toString());
                    System.out.println(tmp.toString());
                    mines.clear();
                    for(l=0;l<tmp.size();l++)
                    {
                        for(o=0;o<min.size();o++)
                        {
                            if(min.elementAt(o).equalsIgnoreCase(tmp.elementAt(l)))
                            {
                                mines.add(min.elementAt(o));
                                y++;
                            }
                        }
                    }
                }
                else {mines.addAll(tmp); start=true;}
            } catch (SQLException ex) {
                Logger.getLogger(Reason.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        /*String[][] gg = new String[1][];
        gg[0]=(String[]) mines.toArray();
        String[] cl = { "col1"} ;
        JTable a = new JTable(gg,cl);*/
    }

}
