/* Author: Amay Agrawal
 * Project: Bacteria Cloud
 * Mentor: Prof. Manish K Gupta
 */
import java.util.Random;
import java.io.*;
public class Encode1 {
	public static void main(String [] args) throws IOException{
		File f = new File("C:\\Users\\HP\\Desktop\\abcd.txt");
		byte[] fileData = new byte[(int) f.length()];
		FileInputStream in = new FileInputStream(f);
		in.read(fileData);
		in.close();
		Random rand = new Random();
		int counter1=1;
		int counter = 0;
		int counter2=0;
		int flag=0;
		String allocation[] = new String [256];
		StringBuilder binary = new StringBuilder();
		for(byte b : fileData){
			int val = b;
			//System.out.println("value of : "+val);
			for (int i = 0; i < 8; i++)
		     {
		        binary.append((val & 128) == 0 ? 0 : 1);
		        val <<= 1;
		     }
		}
		System.out.println(binary);
		int length1 = binary.length();
		if(length1<97){
			for(int i=0;i<(100-length1);i++){
				binary.append(0);
			}
			length1 = binary.length();
		}
		int length2 = length1%32;
		for(int y=0;y<32-length2;y++){
			binary.append(0);
		}
		
		/*String output = "";
		for(int i = 0; i <= binary.length() - 8; i+=8)
		{
		    int k = Integer.parseInt(binary.substring(i, i+8), 2);
		    //System.out.println(k);
		    output += (char) k;
		}  
		//int charCode = Integer.parseInt(content, 2);
		//String str = new Character((char)charCode).toString();
		System.out.println("Final String : "+output);*/
		String check[][] = new String[100][2];
		String check1 [] = new String[100];
		int j=0;
		for(int i = 0; i <= binary.length() - 32; i+=32)
		{
		   String abcd = binary.substring(i, i+32);
		   counter++;
		   check[j++][0]=abcd;
		}
		System.out.println("or bhao "+j);
		//System.out.println(counter);
		for(int k=0;k<j;k++){
			System.out.println(check[k][0]);
		}
		System.out.println("amya");
		
		int counter10 = (int) (counter + (int) counter*0.4);
		
		for(int m=0;m<counter10;m++){
			int random1 = (int) (Math.random()*counter);
			int random2 = (int) (Math.random()*counter);
			while(true){
			if(random2==random1)
				random2 = (int) (Math.random()*counter);
			else{
				break;
			}
			}
			int random3 = (int) (Math.random()*counter);
			while(true){
			if(random3==random2 || random3==random1)
				random3 = (int) (Math.random()*counter);
			else 
				break;
			}
			int random4 = (int) (Math.random()*counter);
			while(true){
				if(random4==random2 || random4==random1 || random4==random3)
					random4 = (int) (Math.random()*counter);
				else 
					break;
				}
			//System.out.println(random1+" "+random2+" "+random3);
			String ab = check[m%counter][0];
			String ac = check[(m+1)%counter][0];
			String ad = check[(m+2)%counter][0];
			String as = check[(m+3)%counter][0];
			
			String ae = Integer.toString(m%counter);
			String af = Integer.toString((m+1)%counter);
			String ag = Integer.toString((m+2)%counter);
			String ak = Integer.toString((m+3)%counter);
			
			String append = ae+","+af+","+ag+","+ak;
			
			StringBuilder sb = new StringBuilder();

		    for (int i = 0; i < ab.length(); i++) {
		        sb.append(charOf(bitOf(ab.charAt(i)) ^ bitOf(as.charAt(i)) ^ bitOf(ac.charAt(i))
		                ^ bitOf(ad.charAt(i))));
		    }

		    String result = sb.toString();
		    String a = Integer.toBinaryString(counter1);
		    allocation[counter1] = append;
		    int length = a.length();
		    int difff= 8-length;
		    String final1 = "";
		    for(int h=0;h<difff;h++){
		    	final1+='0';
		    }
		    final1+=a;
		    counter1++;
		    String final2 = convert(final1+result);
		    
		    for(int u=1;u<final2.length()-3;u++){
		    	char pq = final2.charAt(u);
		    	char rs = final2.charAt(u+1);
		    	char tu = final2.charAt(u+2);
		    	
		    	if(pq == rs && rs == tu){
		    		flag=1;
		    		break;
		    	}
		    	else{
		    		flag=0;
		    	}
		    	
		    }
		    int counterg=0;
	    	int counterc=0;
		    for(j=0;j<final2.length();j++){
		    	//int countera=0;
		    	
		    	//int countert=0;
		    	if(final2.charAt(j)=='G'){
		    		counterg++;
		    	}
		    	else if(final2.charAt(j)=='C'){
		    		counterc++;
		    	}
		    	else{
		    		continue;
		    	}
		    	
		    }
		    System.out.println(counterg+" "+counterc);
		    int finalcounter = counterg+counterc;
		    double tr1 = (double) finalcounter;
		    double tr2 = (double) final2.length();
		    System.out.println(finalcounter+" "+final2.length());
		    double pq = tr1/tr2;
		    System.out.println(pq);
		    
		    //double rs = counterc/final2.length();
		    counterg=0;
		    counterc=0;
		    if(flag==0 && ((pq<=0.8)) ){
		    check1[counter2++] = final2;
		    System.out.println((pq)+" "+final2);
		    }
		    //pq=0;
		    //rs=0;
		    flag=0;
		    System.out.println("how r u??");
		    
		}
		for(int y=0;y<counter1;y++){
			System.out.println(allocation[y]);
		}
		decode(counter2,check1,check,allocation);
	}
	
	private static boolean bitOf(char in) {
	    return (in == '1');
	}

	private static char charOf(boolean in) {
	    return (in) ? '1' : '0';
	}
	
	public static String convert(String ab){
		String cd="";
		char bc = ab.charAt(0);
		if(bc=='0'){
			cd+='A';
		}
		else{
			cd+='T';
		}
		
		for(int i=1;i<ab.length();i++){
			char ef = cd.charAt(i-1);
			char gh = ab.charAt(i);
			if(ef=='A'){
				if(gh=='1'){
					cd+='G';
				}
				else{
					cd+='T';
				}
			}
			else if(ef=='T'){
				if(gh=='1'){
					cd+='C';
				}
				else{
					cd+='A';
				}
			}
			else if(ef=='G'){
				if(gh=='1'){
					cd+='T';
				}
				else{
					cd+='C';
				}
			}
			else if(ef=='C'){
				if(gh=='1'){
					cd+='A';
				}
				else{
					cd+='G';
				}
			}
			
		}
		return cd;
		
	}
	
	public static void decode (int counter,String [] check,String check1[][],String allocation[]){
		
		System.out.println("In decoder");
		System.out.println("1");
		System.out.println(counter);
		System.out.println("2");
		for(int k=0;k<check1.length;k++){
			System.out.println(check1[k][0]);
		}
		System.out.println("3");
		
		for(int yu=0;yu<check.length;yu++){
			System.out.println(check[yu]);
		}
		String final1 = "";
		int counter5=1;
		int counter6=0;
		System.out.println("heya "+counter);
		String check2 [] = new String[100];
		int completed [] = new int[counter];
		int nice=0;
		for(int k=0;k<counter;k++){
				System.out.println(k);
				final1="";
			 String ab = check[k];
			 if(ab==null){
				 continue;
			 }
			 //String cd="";
			 char bc = ab.charAt(0);
				if(bc=='A'){
					final1+='0';
				}
				else{
				    final1+='1';
				}
				//System.out.println(cd);
				//System.out.println(ab);
				for(int i=1;i<ab.length();i++){
					char ef = ab.charAt(i-1);
					char gh = ab.charAt(i);
					//System.out.println(ef+" "+gh);
					if(ef=='A'){
						if(gh=='G'){
							final1+='1';
						}
						else{
							final1+='0';
						}
					}
					else if(ef=='T'){
						if(gh=='C'){
							final1+='1';
						}
						else{
							final1+='0';
						}
					}
					else if(ef=='G'){
						if(gh=='T'){
							final1+='1';
						}
						else{
							final1+='0';
						}
					}
					else if(ef=='C'){
						if(gh=='A'){
							final1+='1';
						}
						else{
							final1+='0';
						}
					}
				}
				
			System.out.println("hey");
			System.out.println(final1);
			String seed = final1.substring(0, 8);
			int h=0;
			for(h=0;h<seed.length();h++){
				if(seed.charAt(h)=='0'){
					continue;
				}
				else{
					break;
				}
			}
			String seed1 = seed.substring(h, seed.length());
			int number = Integer.parseInt(seed1,2);
			System.out.println("seed "+number);
			
			String decode1 = allocation[number];
			String parts[] = decode1.split(",");
			int ap = Integer.parseInt(parts[0]);
			int aq = Integer.parseInt(parts[1]);
			int ar = Integer.parseInt(parts[2]);
			int as = Integer.parseInt(parts[3]);
			
			System.out.println("three numbers "+ap+" "+aq+" "+ar+" "+as);
			for(int gh=0;gh<counter;gh++){
				System.out.print(completed[gh]+" ");
			}
			String ac = final1.substring(8, 40);
			String ad="";
			String ae="";
			String af="";
			
			if(completed[ap]==0){
			
			//String ad = check1[counter5%counter][0];
			//String ae = check1[(counter5+1)%counter][0];
				counter6=ap;
				ad = check1[aq][0];
				ae = check1[ar][0];
				af = check1[as][0];
				completed[ap]=1;
			
			}
			else if (completed[aq]==0){
				counter6=aq;
				ad = check1[ap][0];
				ae = check1[ar][0];
				af = check1[as][0];
				completed[aq]=1;
			}
			
			else if (completed[ar]==0){
				counter6=ar;
				ad = check1[ap][0];
				ae = check1[aq][0];
				af = check1[as][0];
				completed[ar]=1;
			}
			
			else if (completed[as]==0){
				counter6=as;
				ad = check1[ap][0];
				ae = check1[aq][0];
				af = check1[ar][0];
				completed[as]=1;
			}
			else if (completed[as]==1 && completed[ap]==1 && completed[aq]==1 && completed[ar]==1){
				continue;
			}
			
			StringBuilder sb = new StringBuilder();

		    for (int i = 0; i < ac.length(); i++) {
		    	//System.out.println(ac.length()+" "+ad.length()+" "+ae.length());
		        sb.append(charOf(bitOf(ac.charAt(i)) ^ bitOf(af.charAt(i)) ^ bitOf(ad.charAt(i))
		                ^ bitOf(ae.charAt(i))));
		    }

		    String result = sb.toString();
			check2[counter6] = result;
			counter5++;
			final1="";
		}
		System.out.println("kaisa hai");
		String z="";
		for(int g=0;g<4;g++){
			z+=check2[g];
		}
		String output = "";
		for(int i = 0; i <= z.length() - 8; i+=8)
		{
		    int k = Integer.parseInt(z.substring(i, i+8), 2);
		    if(k==0)
		    	continue;
		    //System.out.println(k);
		    output += (char) k;
		}  
		//int charCode = Integer.parseInt(content, 2);
		//String str = new Character((char)charCode).toString();
		System.out.println("Final String : "+output);
	}
}
