#include <iostream>
#include <fstream>
#include "stdio.h"
#include "string.h"

#define STRING_LENGTH 120
#define MAX_SIZE 4096

using namespace::std;


void encript(char *UnEncriptArray, char EncriptArray[], int Key, int stringLength); 
void decript(char *EncriptArray, char UnEncriptArray[], int Key, int stringLength);


int main (int argc, char * const argv[]) {
	char inFileName[STRING_LENGTH];
	char outFileName[STRING_LENGTH];
	char arrayIn[MAX_SIZE];
	char arrayOut[MAX_SIZE];
		
	int Key = 17;
	
	
	cout << "Welcome to <Encrypt/Decrypt>!!! \n" << endl;
	
	// DECODE FUNCTION
    
    cout << "You are in Decrypt Mode. \n" << endl;
	
	//asks user for file names
	cout << "Please enter the name of the file to get text from:" << endl;
	cin >> inFileName;
	cout << "Please enter the name of the file to save text to:" << endl;
	cin >> outFileName;
	
	//creates file streams from file names given above
	ifstream dataIn(inFileName, ios::in);
	ofstream dataOut(outFileName, ios::out);
	
		//FOR TESTING ONLY
	//prints status of file streams
	cout << "In File: " << inFileName << dataIn.is_open() << endl;
	cout << "Out File: " << outFileName << dataOut.is_open() << endl;
	
	//loads data from input location into data array
	int positionIn = 0; 
	char positionChar;
	while (!dataIn.eof()) {
		dataIn >> positionChar;
		positionChar = toupper(positionChar);
		arrayIn[positionIn] = positionChar;
		positionIn++;
	}
	arrayIn[positionIn - 1] = 0;
	
	//decrypt data
	cout << "Please enter the key value <int>: " << endl;
	//scanf("%d", &Key);
	//cin >> Key;
	decript(arrayIn, arrayOut, Key, strlen(arrayIn));
	cout << "The key decrypted the text as: " << endl;
	cout << arrayOut << endl;
	
	//output data
	dataOut << "Output from Encrypt/Decrypt." << endl;
	dataOut << "Decryption Key Used: ";
	dataOut << Key << endl;
	dataOut << "The decrypted text is: ";
	dataOut << arrayOut << endl;
	
	
	//close file i/o and exit program
	cout << "Thank You for using Encrypt/Decrypt. Your data has been saved to your output file.";
	
	dataIn.close();
	dataOut.close();
	
	return 0;

}



void encript(char *UnEncriptArray, char EncriptArray[], int Key, int stringLength) {
	int encodeWorking;
	int wrapValue;
	
	for (int i = 0; i < stringLength; i++) {
		encodeWorking = (int)UnEncriptArray[i] + Key;
		if (encodeWorking > 90) {
			wrapValue = encodeWorking - 90;
			encodeWorking = 64 + wrapValue;
		}
		
		EncriptArray[i] = (char)encodeWorking;
		
	}
	
}

void decript(char *EncriptArray, char UnEncriptArray[], int Key, int stringLength) {
	int decodeWorking;
	int wrapValue;
	
	for (int i = 0; i < stringLength; i++) {
		decodeWorking = (int)EncriptArray[i] - Key;
		if (decodeWorking < 64) {
			wrapValue = 64 - decodeWorking;
			decodeWorking = 90 - wrapValue;
		}
		
		UnEncriptArray[i] = (char)decodeWorking;
		
	}
	
}


/*
int done(int userInput) {
	if ((userInput == 89) || (userInput == 121)) {
		return 1;
	}
	else {
		return 0;
	}
	
}
*/






