package com.backfire.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.UUID;

import android.content.Context;
import android.os.Environment;

public class Installation
{
	private final String INSTALLATION = "INSTALLATION";
	private byte[] sync = new byte[0];
	private final String LOG_FILE = "client";
	private String LOG_PATH = "android" + File.separator + "clientid" + File.separator + "session";

	public String getClientID(Context context)
	{
		String sID = null;
	
		try
		{
			File installation = new File(context.getFilesDir(), INSTALLATION);
			if (!installation.exists())
			{
				String sdValue = readSDFile();
				if (sdValue == null || sdValue.equals(""))
				{
					writeInstallationFile(installation);
					sID = readInstallationFile(installation);
					writeSDFile(sID);
				} else
				{
					sID = sdValue;
					writeInstallationFile(installation, sID);
				}
			} else
			{
				sID = readInstallationFile(installation);
				String sdValue = readSDFile();
				if (sdValue == null || sdValue.equals(""))
				{
					writeSDFile(sID);
				} else if (!sID.equals("sdValue"))
				{
					sID = sdValue;
					writeSDFile(sID);
				}

			}
		} catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return sID;
	}

	private String readInstallationFile(File installation) throws IOException
	{
		RandomAccessFile f = new RandomAccessFile(installation, "r");
		byte[] bytes = new byte[(int) f.length()];
		f.readFully(bytes);
		f.close();
		return new String(bytes);
	}

	private void writeInstallationFile(File installation) throws IOException
	{
		FileOutputStream out = new FileOutputStream(installation, false);
		String id = UUID.randomUUID().toString();
		out.write(id.getBytes());
		out.close();
	}

	private static void writeInstallationFile(File installation, String id) throws IOException
	{
		FileOutputStream out = new FileOutputStream(installation, false);
		// 当参数为空或为true时，是在源文件上进行追加，为false时是覆盖重写
		out.write(id.getBytes());
		out.close();
	}

	private String readSDFile() throws IOException
	{
		if (false == testSDCard())
			return null;
		String path = getLogPath() + File.separator + LOG_FILE;
		File file = new File(path);
		if (!file.exists())
			return null;
		BufferedReader reader = null;
		String readmessage = null;
		try
		{
			reader = new BufferedReader(new FileReader(file));
			readmessage = reader.readLine();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			if (reader != null)
				reader.close();
		}
		return readmessage;
	}

	private void writeSDFile(String info)
	{
		synchronized (sync)
		{
			if (false == testSDCard())
				return;
			try
			{
				String path = getLogPath() + File.separator + LOG_FILE;
				File file = new File(path);
				if (!file.exists())
					file = createFile();
				FileOutputStream fos = new FileOutputStream(file, false);
				// 当参数为空或为true时，是在源文件上进行追加，为false时是覆盖重写
				fos.write(info.getBytes());
				fos.close();
			} catch (IOException ex)
			{
			}
		}
	}

	private boolean testSDCard()
	{
		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
		{
			return true;
		}
		return false;
	}

	private String getLogPath()
	{
		return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + LOG_PATH;
	}

	private File createFile() throws IOException
	{
		String path = getLogPath() + File.separator + LOG_FILE;
		File file = new File(getLogPath());
		file.mkdirs();
		file = new File(path);
		file.createNewFile();
		return file;
	}
}
