// This is for recording the data

char digit2char(int digit)
{
	switch (digit) {
		case 0:
			return '0';
			break;
		case 1:
			return '1';
			break;
		case 2:
			return '2';
			break;
		case 3:
			return '3';
			break;
		case 4:
			return '4';
			break;
		case 5:
			return '5';
			break;
		case 6:
			return '6';
			break;
		case 7:
			return '7';
			break;
		case 8:
			return '8';
			break;
		case 9:
			return '9';
			break;
		default:
			break;
	}
	
	
}

void Record_Verticies(double *V_x, double *V_y, double *V_z, int** V_c, int N, char m, char* filename)
{
	ofstream myfile;
	myfile.open(filename, ios::app);
	
	myfile<<"V_x_"<<m<<" = [";
	for (int i=0; i<N-1; i++) {
		myfile<<V_x[i]<<", ";
	}
	myfile<<V_x[N-1]<<"];"<<endl;
	
	myfile<<"V_y_"<<m<<" = [";
	for (int i=0; i<N-1; i++) {
		myfile<<V_y[i]<<", ";
	}
	myfile<<V_y[N-1]<<"];"<<endl;
	
	myfile<<"V_z_"<<m<<" = [";
	for (int i=0; i<N-1; i++) {
		myfile<<V_z[i]<<", ";
	}
	myfile<<V_z[N-1]<<"];"<<endl;
	
	myfile<<"V_c = cell(" << N << ", 1);";
	for (int i=0; i<N; i++) {
		myfile<<"V_c{"<<i+1<<"} = ["<< V_c[i][0] << ", " << V_c[i][1] << ", " << V_c[i][2] << ", " << V_c[i][3] << "];" << endl;
	}
	
	myfile.close();
}

void Record_Vert_data(double *V_x, double *V_y, double *V_z, int N, int c, char* filename)
{
	ofstream myfile;
	myfile.open(filename, ios::app);
	
	myfile<<"V_x = [";
	for (int i=0; i<N-1; i++) {
		myfile<<V_x[i]<<", ";
	}
	myfile<<V_x[N-1]<<"];"<<endl;
	
	myfile<<"V_y = [";
	for (int i=0; i<N-1; i++) {
		myfile<<V_y[i]<<", ";
	}
	myfile<<V_y[N-1]<<"];"<<endl;
	
	myfile<<"V_z = [";
	for (int i=0; i<N-1; i++) {
		myfile<<V_z[i]<<", ";
	}
	myfile<<V_z[N-1]<<"];"<<endl;
	myfile<<"V_cell{"<<c<<"} = [V_x;V_y;V_z];"<<endl;
	
	myfile.close();
}

void Record_Energy_data(double *E_A, double *E_B, double *E_L, double *E_V,  double *E_P, int Nc, int Nv, int c, char* filename)
{
	ofstream myfile;
	myfile.open(filename, ios::app);
	
	myfile<<"E_A_cell{"<<c<<"} = [";
	for (int i=0; i<Nc-1; i++) {
		myfile<<E_A[i]<<", ";
	}
	myfile<<E_A[Nc-1]<<"];"<<endl;
	
	myfile<<"E_B_cell{"<<c<<"} = [";
	for (int i=0; i<Nc-1; i++) {
		myfile<<E_B[i]<<", ";
	}
	myfile<<E_B[Nc-1]<<"];"<<endl;
	
	myfile<<"E_V_cell{"<<c<<"} = [";
	for (int i=0; i<Nc-1; i++) {
		myfile<<E_V[i]<<", ";
	}
	myfile<<E_V[Nc-1]<<"];"<<endl;
	
	myfile<<"E_L_cell{"<<c<<"} = [";
	for (int i=0; i<Nc-1; i++) {
		myfile<<E_L[i]<<", ";
	}
	myfile<<E_L[Nc-1]<<"];"<<endl;
	
	myfile<<"E_P_cell{"<<c<<"} = [";
	for (int i=0; i<Nv-1; i++) {
		myfile<<E_P[i]<<", ";
	}
	myfile<<E_P[Nv-1]<<"];"<<endl<<endl;
	
	myfile.close();
}

void Record_All_Data_new_file(double *V_x, double *V_y, double *V_z, int N, int count_tmp)
{
	char* record_file = new char[18];
	int count1, count2, count3;
	
	count1 = count_tmp/100;
	count2 = (count_tmp - count1*100)/10;
	count3 = count_tmp - count1*100 - count2*10;
	
	if (count1 >= 10 || count2 >= 10 || count3 >= 10) {
		cout << "count is wrong" << endl;
	}
	
	record_file[0] = 'd';
	record_file[1] = 'a';
	record_file[2] = 't';
	record_file[3] = 'a';
	record_file[4] = 'o';
	record_file[5] = 'u';
	record_file[6] = 't';
	record_file[7] = '_';
	record_file[8] = '0';
	record_file[9] = '_';
	record_file[10] = '0';
	record_file[11] = '_';
	record_file[12] = digit2char(count1);
	record_file[13] = digit2char(count2);
	record_file[14] = digit2char(count3);
	record_file[15] = '.';
	record_file[16] = 'm';
	record_file[17] = '\0';
	
	cout << "Record file is " << record_file << endl;
	
	ofstream myfile;
	myfile.open(record_file, ios::out);
	
	myfile<<"V_x = [";
	for (int i=0; i<N-1; i++) {
		myfile<<V_x[i]<<", ";
	}
	myfile<<V_x[N-1]<<"];"<<endl;
	
	myfile<<"V_y = [";
	for (int i=0; i<N-1; i++) {
		myfile<<V_y[i]<<", ";
	}
	myfile<<V_y[N-1]<<"];"<<endl;
	
	myfile<<"V_z = [";
	for (int i=0; i<N-1; i++) {
		myfile<<V_z[i]<<", ";
	}
	myfile<<V_z[N-1]<<"];"<<endl;
	
	myfile.close();
	//delete[] record_file;
}

void Record_Cell_data(double *C_A, double *C_B, double *C_V, double *C_L_1, double *C_L_2, double *C_L_3, double *C_L_4, double *Yolk, int N, int c, char* filename)
{
	ofstream myfile;
	myfile.open(filename, ios::app);
	
	myfile<<"Apical_cell{"<<c<<"} = [";
	for (int i=0; i<N-1; i++) {
		myfile<<C_A[i]<<", ";
	}
	myfile<<C_A[N-1]<<"];"<<endl;
	
	myfile<<"Basal_cell{"<<c<<"} = [";
	for (int i=0; i<N-1; i++) {
		myfile<<C_B[i]<<", ";
	}
	myfile<<C_B[N-1]<<"];"<<endl;
	
	myfile<<"Volume_cell{"<<c<<"} = [";
	for (int i=0; i<N-1; i++) {
		myfile<<C_V[i]<<", ";
	}
	myfile<<C_V[N-1]<<"];"<<endl;
	
	myfile<<"L1_cell{"<<c<<"} = [";
	for (int i=0; i<N-1; i++) {
		myfile<<C_L_1[i]<<", ";
	}
	myfile<<C_L_1[N-1]<<"];"<<endl;
	
	myfile<<"L2_cell{"<<c<<"} = [";
	for (int i=0; i<N-1; i++) {
		myfile<<C_L_2[i]<<", ";
	}
	myfile<<C_L_2[N-1]<<"];"<<endl;
	
	myfile<<"L3_cell{"<<c<<"} = [";
	for (int i=0; i<N-1; i++) {
		myfile<<C_L_3[i]<<", ";
	}
	myfile<<C_L_3[N-1]<<"];"<<endl;
	
	myfile<<"L4_cell{"<<c<<"} = [";
	for (int i=0; i<N-1; i++) {
		myfile<<C_L_4[i]<<", ";
	}
	myfile<<C_L_4[N-1]<<"];"<<endl;
	
	myfile<<"Yolk_cell{"<<c<<"} = [";
	for (int i=0; i<N-1; i++) {
		myfile<<Yolk[i]<<", ";
	}
	myfile<<Yolk[N-1]<<"];"<<endl;
	
	myfile.close();
}

void Record_Cell_Parameters(double *K_A, double *K_B, double *K_V, double *K_L, double K_Y, int N, char* filename)
{
	ofstream myfile;
	myfile.open(filename, ios::app);
	
	myfile<<"KA_cell = [";
	for (int i=0; i<N-1; i++) {
		myfile<<K_A[i]<<", ";
	}
	myfile<<K_A[N-1]<<"];"<<endl;
	
	myfile<<"KB_cell = [";
	for (int i=0; i<N-1; i++) {
		myfile<<K_B[i]<<", ";
	}
	myfile<<K_B[N-1]<<"];"<<endl;
	
	myfile<<"KV_cell = [";
	for (int i=0; i<N-1; i++) {
		myfile<<K_V[i]<<", ";
	}
	myfile<<K_V[N-1]<<"];"<<endl;
	
	myfile<<"KL_cell = [";
	for (int i=0; i<N-1; i++) {
		myfile<<K_L[i]<<", ";
	}
	myfile<<K_L[N-1]<<"];"<<endl;
	
	myfile<<"KY_cell = ["<<K_Y<<"];"<<endl<<endl;
	
	myfile.close();
}

void Record_Center(double *C_x, double *C_y, double *C_z, int N, char* filename)
{
	ofstream myfile;
	myfile.open(filename, ios::app);
	
	myfile<<"Center_x = [";
	for (int i=0; i<N-1; i++) {
		myfile<<C_x[i]<<", ";
	}
	myfile<<C_x[N-1]<<"];"<<endl;
	
	myfile<<"Center_y = [";
	for (int i=0; i<N-1; i++) {
		myfile<<C_y[i]<<", ";
	}
	myfile<<C_y[N-1]<<"];"<<endl;
	
	myfile<<"Center_z = [";
	for (int i=0; i<N-1; i++) {
		myfile<<C_z[i]<<", ";
	}
	myfile<<C_z[N-1]<<"];"<<endl;
	
	
	myfile.close();
}

void Record_Cells(int** AP_ver, int** BS_ver, int N, char* filename)
{
	ofstream myfile;
	myfile.open(filename, ios::app);
	
	myfile<<"AP_ver_id = cell(" << N << ", 1);";
	for (int i=0; i<N; i++) {
		myfile<<"AP_ver_id{"<<i+1<<"} = ["<<AP_ver[i][0]<<", "<<AP_ver[i][1] <<", "<<AP_ver[i][2] <<", "<<AP_ver[i][3]<< "];"<<endl;
	}
	
	myfile<<"BS_ver_id = cell(" << N << ", 1);";
	for (int i=0; i<N; i++) {
		myfile<<"BS_ver_id{"<<i+1<<"} = ["<< BS_ver[i][0] << ", " << BS_ver[i][1] << ", " << BS_ver[i][2] << ", " << BS_ver[i][3] << "];" << endl;
	}
	
	myfile.close();
}

void Record_Grad(double** Grad, int N, char* filename)
{
	ofstream myfile;
	myfile.open(filename, ios::app);
	
	myfile<<"V_grad = zeros(" << N << ", 3);";
	for (int i=0; i<N; i++) {
		myfile<<"V_grad("<<i+1<<", :) = ["<<Grad[i][0]<<", "<<Grad[i][1] <<", "<<Grad[i][2] << "];"<<endl;
	}
	
	myfile.close();
}

void Record_Init(int Nx, int Ny, int Nv, int Nc, double* P1,  double* P2, double* P3, char* filename)
{		
	ofstream file;
	file.open(filename, ios::out);
	
	file<<"Nx = "<<Nx<<";"<<endl;
	file<<"Ny = "<<Ny<<";"<<endl;
	file<<"Nv = "<<Nv<<";"<<endl;
	file<<"Nc = "<<Nc<<";"<<endl;
	file<<"K_apical = "<<P1[0]<<";"<<endl;
	file<<"K_basal = "<<P1[1]<<";"<<endl;
	file<<"K_volume = "<<P1[2]<<";"<<endl;
	file<<"K_length = "<<P1[3]<<";"<<endl;
	file<<"K_yolk = "<<P3[5]<<";"<<endl;
	file<<"A_0 = "<<P2[0]<<";"<<endl;
	file<<"A_0_c = "<<P3[4]<<";"<<endl;
	file<<"B_0 = "<<P2[1]<<";"<<endl;
	file<<"V_0 = "<<P2[2]<<";"<<endl;
	file<<"L_0 = "<<P2[3]<<";"<<endl;
	file<<"Y_0 = "<<P3[6]<<";"<<endl;
	file<<"y_start = "<<P3[0]<<";"<<endl;
	file<<"y_stop = "<<P3[1]<<";"<<endl;
	file<<"x_start = "<<P3[2]<<";"<<endl;
	file<<"x_stop = "<<P3[3]<<";"<<endl;
	
	file.close();
}
