package com.tsgame.tsgame_niuniu.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import com.game.webgame.view.SpUtil;
import com.tsgame.tsgame_niuniu.system.Myapplication;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;

public class initwebviewutil {
	Context context;

	private SharedPreferences sp;

	String kfile = Settingutil.kfile;

	private Handler hadler;

	public static String destFileDir = null;

	private static initwebviewutil webview = null;

	public static initwebviewutil getInstance() {

		if (webview == null) {
			webview = new initwebviewutil();
		}

		return webview;

	}

	private final OkHttpClient okclient = new OkHttpClient();

	public void sethandler(Handler hadler) {
		this.hadler = hadler;
	}

	public void isfirst() {

		sp = SpUtil.getSharePerference(Myapplication.application);
		Boolean istrue = SpUtil.isFirst(sp);

		if (istrue) {
			hadler.sendEmptyMessage(3);

			System.out.println("不是第一次");
		} else {

			System.out.println("第一次");
			init();

		}
	}

	public void init() {

		sp = SpUtil.getSharePerference(Myapplication.application);

		File sdFile = null;

		sdFile = Myapplication.application.getFilesDir();

		//
		// System.out.println(sdFile.getAbsolutePath());
		//
//		 if (Environment.getExternalStorageState().equals(
//		 Environment.MEDIA_MOUNTED)) {
//		 // 获取SDCard的路径
//		 sdFile = Environment.getExternalStorageDirectory();
//		 // Toast.makeText(context, sdFile.getAbsolutePath(), 1).show();
//		 } else {
//		 sdFile = Environment.getDataDirectory();
//		 // Toast.makeText(context, sdFile.getPath(), 1).show();
//		 } 

		getdate(sdFile);
		SpUtil.setBooleanSharedPerference(sp, "isFirst", true);
		hadler.sendEmptyMessage(3);
	}

	private void getdate(File sdFile) {
		// 方法测试此抽象路径名定义的文件或目录是否存在。
		// mkdir()只会建立一级的文件夹
		
		String path1 = File.separator + Settingutil.filestart;
		
		String path2 = sdFile.getAbsolutePath() + File.separator + "tsgames"
				+ File.separator + Myapplication.application.getPackageName()
				+ File.separator + System.currentTimeMillis();
		
		String path = path2 + path1;
		///data/user/0/com.jx.jyhd/files/tsgames/com.jx.jyhd/1505230174618/frdt0C1GG0t91P0McFo0rbA1he5yurbS/Shuangjian
	
        System.out.println("path="+path);
	

		SpUtil.setStringSharedPerference(sp, "urlpath", path);

		//int end = path.length() - path1.length();

		SpUtil.setStringSharedPerference(sp, "upurlpath",
				path2);
		
		
		
		
		//System.out.println(path.substring(0, end)+"----");
		
		//System.out.println(SpUtil.getStringSharedPerference(sp, "urlpath"));

		sdFile = new File(path);
		if (!sdFile.exists()) {
			sdFile.mkdirs();
			Log.d("ASSETS", sdFile.getAbsolutePath());
		} else {
			Log.d("ASSETS", sdFile.getAbsolutePath());
		}

		try {
			String[] file = Myapplication.application.getAssets().list(kfile);
			if (file.length > 0) {
				for (int i = 0; i < file.length; i++) {
					
					System.out.println("***"+file[i]);
					
					// String filepath = sdFile.getAbsolutePath() +
					// File.separator
					// + file[i];
					listdate(sdFile, file[i]);
				}
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}

	private void listdate(File sdFile, String filepath) {
		String paths = kfile + File.separator + filepath;
		try {
			String[] file = Myapplication.application.getAssets().list(paths);
			System.out.println("file.length="+file.length);
			if (file.length > 0) {
				File sdFiles = new File(sdFile.getAbsolutePath()
						+ File.separator + filepath);
				sdFiles.mkdirs();
				for (int i = 0; i < file.length; i++) {
					System.out.println(file[i]);
					listdate(sdFile, filepath + File.separator + file[i]);
				}
			} else {
				try {
					byte[] buffer;
					int length;
					InputStream inStream;
					FileOutputStream outStream;
					inStream = Myapplication.application.getAssets()
							.open(paths);
					
					outStream = new FileOutputStream(sdFile + File.separator
							+ filepath);
     
					System.out.println(sdFile.getAbsolutePath());
      
					buffer = new byte[1024];
					while ((length = inStream.read(buffer)) > 0) {
						outStream.write(buffer, 0, length);
					}
					inStream.close();
					outStream.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		
		String urlpath1 = SpUtil.getStringSharedPerference(sp, "upurlpath");
		
		String urlpath2 = SpUtil.getStringSharedPerference(sp, "urlpath");
		
		String url = sdFile + File.separator + filepath;
		
		if (filepath.endsWith(".so")) {
			File file = new File(url);
			try {

				System.out.println("解压");
				ZipUtils.upZipFile1(file, urlpath2 + File.separator);
				
			} catch (ZipException e) {

				e.printStackTrace();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}else if(filepath.endsWith(".zip")){
			System.out.println("解压");
			File file = new File(url);

			try {
				ZipUtils.upZipFile1(file, urlpath2 + File.separator);

			} catch (ZipException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
		delet(url);
	}

	private void delet(String url) {
		
		File dir1 = new File(url);

		if (dir1.exists()) {
			dir1.delete();
		}
		
	}
}
