package com.realnet.utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class RealNetUtils {

	public static String stringReplace(String str, String start, String end, String replaceWith, String file_type) {
		int i = str.indexOf(start);
		while (i != -1) {
			int j = str.indexOf(end, i + 1);
			if (j != -1) {
				/*
				 * @Include starting and ending string String data = str.substring(0, i +
				 * start.length()) + "\n" + replaceWith + "\n"; String temp = str.substring(j);
				 * 
				 * @Not Include starting and ending string String data = str.substring(0, i) +
				 * "\n" + replaceWith + "\n"; String temp = str.substring(j + end.length());
				 */
				String data = str.substring(0, i) + "\n" + replaceWith + "\n";
				String temp = str.substring(j + end.length());
				data += temp;
				str = data;
				i = str.indexOf(start, i + replaceWith.length() + end.length() + 1);
			} else {
				break;
			}
		}

//		if (replaced) {
//			String newStart = "";
//			String newEnd = "";
//			if(file_type.equals("html") || file_type.equals("jsp")) {
//				newStart = "<!-- bcf-fieldloop-startshere-processed -->";
//				newEnd = "<!-- bcf-fieldloop-endshere-processed -->";
//				str = str.replace(start, newStart);
//				str = str.replace(end, newEnd);
//			}
//			if(file_type.equals("java") || file_type.equals("ts") || file_type.equals("js")) {
//				newStart = "/* bcf-fieldloop-startshere-processed */";
//				newEnd = "/* bcf-fieldloop-endshere-processed */";
//				str = str.replace(start, newStart);
//				str = str.replace(end, newEnd);
//			}
//		}
		return str;
	}

	public static String stringReplace(String str, String start, String end, String replaceWith) {
		int i = str.indexOf(start);
		while (i != -1) {
			int j = str.indexOf(end, i + 1);
			if (j != -1) {
				/*
				 * @Include starting and ending string String data = str.substring(0, i +
				 * start.length()) + "\n" + replaceWith + "\n"; String temp = str.substring(j);
				 * 
				 * @Not Include starting and ending string String data = str.substring(0, i) +
				 * "\n" + replaceWith + "\n"; String temp = str.substring(j + end.length());
				 */
				String data = str.substring(0, i) + "\n" + replaceWith + "\n";
				String temp = str.substring(j + end.length());
				data += temp;
				str = data;
				i = str.indexOf(start, i + replaceWith.length() + end.length() + 1);
			} else {
				break;
			}
		}
		return str;
	}

	// file and folder present in a zip file
	public static void printFileList(String filePath) {
		FileInputStream fis = null;
		ZipInputStream zipIs = null;
		ZipEntry zEntry = null;
		try {
			fis = new FileInputStream(filePath);
			zipIs = new ZipInputStream(new BufferedInputStream(fis));
			while ((zEntry = zipIs.getNextEntry()) != null) {
				System.out.println(zEntry.getName());
			}
			zipIs.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * SPRING_MVC_HIBERNATE_MYSQL (sphmy), ANGULAR_SPRING_BOOT_MYSQL (aspmy),
	 * REACT_REACT_NATIVE_MYSQL (rrnmy), REACT_REACT_NATIVE_MONGODB (rrnmo),
	 * ANGULAR_SPRING_BOOT_MONGODB (aspmo), PHP_LARAVEL_MYSQL (plvmy), MEAN (mean)
	 */

	public static String toFirstUpperCase(String input) {
		String output = input.substring(0, 1).toUpperCase() + input.substring(1);
		return output;
	}

	public static String formatStringtoSql(String input) {
		if (input == null || input.isEmpty()) {
			return input;
		}

		// Convert input to lowercase and replace spaces with underscores
		String result = input.trim().replace(" ", "_");

		// Use StringBuilder to modify the result string
		StringBuilder formattedString = new StringBuilder(result);

		// Iterate through each character in the string
		for (int i = 0; i < formattedString.length(); i++) {
			char currentChar = formattedString.charAt(i);

			// Check if the current character is an underscore or space
			if (currentChar == '_') {
				continue;
			}

			// Check if the previous character was an underscore
			if (i > 0 && formattedString.charAt(i - 1) == '_') {
				formattedString.setCharAt(i, Character.toLowerCase(currentChar));
			} else if (Character.isUpperCase(currentChar)) {
				// Insert an underscore before capital letters (except the first character)
				if (i > 0) {
					formattedString.insert(i, '_');
					formattedString.setCharAt(i + 1, Character.toLowerCase(currentChar));
					i++; // Skip the next character which is already processed
				}
			}
		}

//	Example	 Gaurav Kumar gaurav_kumar  GauravKumar gaurav_kumar gaurav_Kumar

		return formattedString.toString();
	}

	public String formatString(String input) {
		if (input == null || input.isEmpty()) {
			return input;
		}

		// Convert input to lowercase and replace spaces with underscores
		String result = input.trim().replace(" ", "_");

		// Use StringBuilder to modify the result string
		StringBuilder formattedString = new StringBuilder(result);

		// Iterate through each character in the string
		for (int i = 0; i < formattedString.length(); i++) {
			char currentChar = formattedString.charAt(i);

			// Check if the current character is an underscore or space
			if (currentChar == '_') {
				continue;
			}

			// Check if the previous character was an underscore
			if (i > 0 && formattedString.charAt(i - 1) == '_') {
				formattedString.setCharAt(i, Character.toLowerCase(currentChar));
			} else if (Character.isUpperCase(currentChar)) {
				// Insert an underscore before capital letters (except the first character)
				if (i > 0) {
					formattedString.insert(i, '_');
					formattedString.setCharAt(i + 1, Character.toLowerCase(currentChar));
					i++; // Skip the next character which is already processed
				}
			}
		}

		return formattedString.toString();
	}
}
