package com.teamBAGO.Interface.Login;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

import android.os.Environment;

public class Login_LocalAccount {
	String root  = Environment.getExternalStorageDirectory().toString() + "/team BAGO/account/local/";
	private BufferedReader bufferReader;

	//Assign Local ID
	public final int assign_ID(){
		Random rand = new Random();
		int local_id = rand.nextInt(999999);
		return local_id;
	}

	//If directory is not exists, create the directory.
	public boolean check_directory_exists(){
		File directory = new File(root);
		if(!directory.exists()){
			directory.mkdirs();
			return false;
		}
		else return true;
	}

	//Save Identification Data
	public boolean save_LocalID(String local_id){
		//Create local ID dat file
		File trySave = new File(root+"local_id.dat");
		try{
			FileOutputStream fos = new FileOutputStream(trySave);
			fos.write(local_id.getBytes());
			fos.close();
		}catch(IOException e){
			return false;
		}
		return true;
	}

	//Read Identification Data
	public int take_LocalID(){
		String str, id="";
		try{
			FileInputStream fis = new FileInputStream(root+"local_id.dat");
			bufferReader = new BufferedReader(new InputStreamReader(fis));
			while((str=bufferReader.readLine())!=null)
				id+=str;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return Integer.parseInt(id);
	}
	
	//Change State
	public boolean change_state(){
		String state = "Logged in";
		File trySave = new File(root+"login_state.dat");
		try{
			FileOutputStream fos = new FileOutputStream(trySave);
			fos.write(state.getBytes());
			fos.close();
		}catch(IOException e){
			return false;
		}
		return true;
	}

}
