package main_package;

import java.io.Serializable;
import java.sql.Connection;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.mysql.jdbc.PreparedStatement;

import main_package.DataBaseConnection;

@ManagedBean(name="insertpage")
@ViewScoped


public class insertPage implements Serializable{

	String pageName, pageVal , groupName ;

	
	public void insertStaticData() {
    
	    String sql = "INSERT INTO page_table (page_name, page_val, group_val) VALUES (?, ?, ?)";
        
	    try 
	    (
	        Connection conn = DataBaseConnection.JAVAConnection();
	        java.sql.PreparedStatement ps = conn.prepareStatement(sql)
	    ) {
        
	        // 1
	        if (!isDuplicate(conn, "businessReport.xhtml", "Report Module 2")) {
	            ps.setString(1, "businessReport.xhtml");
	            ps.setString(2, "Business Report");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        // 2
	        if (!isDuplicate(conn, "businessPerformanceReport.xhtml", "Report Module 2")) {
	            ps.setString(1, "businessPerformanceReport.xhtml");
	            ps.setString(2, "Business Performance Report");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        // 3
	        if (!isDuplicate(conn, "branchReport.xhtml", "Report Module 2")) {
	            ps.setString(1, "branchReport.xhtml");
	            ps.setString(2, "Branch Report");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        // 4
	        if (!isDuplicate(conn, "branchAnalysisReport.xhtml", "Report Module 2")) {
	            ps.setString(1, "branchAnalysisReport.xhtml");
	            ps.setString(2, "Branch Analysis Report");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        // 5
	        if (!isDuplicate(conn, "combineCdsReport.xhtml", "Report Module 2")) {
	            ps.setString(1, "combineCdsReport.xhtml");
	            ps.setString(2, "Combine CDS Report");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        // 6
	        if (!isDuplicate(conn, "centerMeetingReport.xhtml", "Report Module 2")) {
	            ps.setString(1, "centerMeetingReport.xhtml");
	            ps.setString(2, "Center Meeting Report");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        // 7
	        if (!isDuplicate(conn, "centerWiseCollectionReport.xhtml", "Report Module 2")) {
	            ps.setString(1, "centerWiseCollectionReport.xhtml");
	            ps.setString(2, "Center Wise Collection");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        // 8
	        if (!isDuplicate(conn, "centerWiseCollectionAnalysis.xhtml", "Report Module 2")) {
	            ps.setString(1, "centerWiseCollectionAnalysis.xhtml");
	            ps.setString(2, "Center Demand-Collection");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        // 9
	        if (!isDuplicate(conn, "logReport.xhtml", "Report Module 2")) {
	            ps.setString(1, "logReport.xhtml");
	            ps.setString(2, "Log Report");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        // 10
	        if (!isDuplicate(conn, "loanSettlementReport.xhtml", "Report Module 2")) {
	            ps.setString(1, "loanSettlementReport.xhtml");
	            ps.setString(2, "Loan Settlement Report");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        // 11
	        if (!isDuplicate(conn, "queryReport.xhtml", "Report Module 2")) {
	            ps.setString(1, "queryReport.xhtml");
	            ps.setString(2, "Query Report");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        // 12
	        if (!isDuplicate(conn, "moneyLendingQuatarlyReport.xhtml", "Report Module 2")) {
	            ps.setString(1, "moneyLendingQuatarlyReport.xhtml");
	            ps.setString(2, "Money Lending Quart Report");
	            ps.setString(3, "Report Module 2");
	            ps.addBatch();
	        }

	        int[] rows = ps.executeBatch();
	        FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO,
	                        "New records inserted successfully." , "Success"));

	        System.out.println(rows.length + " new records inserted successfully");

	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	private boolean isDuplicate(Connection conn, String pageName, String groupName) {

	    String checkQuery = "SELECT COUNT(*) FROM page_tables WHERE page_name=? AND group_val=?";

	    try (java.sql.PreparedStatement checkPs = conn.prepareStatement(checkQuery)) {

	        checkPs.setString(1, pageName);
	        checkPs.setString(2, groupName);
            
	        java.sql.ResultSet rs = checkPs.executeQuery();
            
	        if (rs.next() && rs.getInt(1) > 0) {
                
	            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
	            		pageName +  " is already available in " + groupName,"Duplicate"));
                
	            return true;
	        }
            
	    }
	    catch (Exception e)
	    {
	        e.printStackTrace();
	    }

	    return false;
	}
	
	

//GETTER AND SETTER
	public String getPageName() {
		return pageName;
	}


	public void setPageName(String pageName) {
		this.pageName = pageName;
	}


	public String getPageVal() {
		return pageVal;
	}


	public void setPageVal(String pageVal) {
		this.pageVal = pageVal;
	}


	public String getGroupName() {
		return groupName;
	}


	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}
	
	
	
	
	
	
}