package com.qy.ui;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import com.qy.Interfaces.SmaliApkToolsPath;
import com.qy.utils.SmaliUtils;

public class OneWayTools {
	public static void main(String[] args) {
		new OneWayTools(new File("OnewaySdk.jar"));
	}

	public OneWayTools(File onwsdk) {
		// TODO Auto-generated constructor stub
		start(onwsdk);
	}
	File onejar;
	private void start(File onewayjar) {
		onejar=onewayjar;
		SmaliUtils smali = new SmaliUtils();
		smali.unPack(onewayjar.getAbsolutePath(), new SmaliApkToolsPath() {

			@Override
			public void smaliPath(String path) {
				// TODO Auto-generated method stub
				update(path);

			}
		});
	}

	private void update(String path) {
		File activity = new File(path + File.separator + "smali" + File.separator + "mobi" + File.separator + "oneway"
				+ File.separator + "sdk" + File.separator + "port" + File.separator + "Unit.smali");
		File provider = new File(path + File.separator + "smali" + File.separator + "mobi" + File.separator + "oneway"
				+ File.separator + "sdk" + File.separator + "b" + File.separator + "c.smali");
		if (!activity.exists()) {
			System.out.println(activity.getAbsolutePath());
			new TextToDalog("警告", "Unit.smali文件不存在!");
			return;
		}
		if (!provider.exists()) {
			new TextToDalog("警告", "c.smali文件不存在!");
			return;
		}

		updateActivity(activity.getAbsolutePath());
		updateProvider(provider.getAbsolutePath());
		SmaliUtils smali = new SmaliUtils();
		smali.setPath(path);
		smali.bale(path, path+File.separator+onejar.getName());

	}

	private void updateActivity(String path) {
		try {
			File file = new File(path);
			BufferedReader reader = new BufferedReader(new FileReader(file));

			String tmp, str = "";
			boolean write = false;
			while ((tmp = reader.readLine()) != null) {
				if (tmp.trim().equals("const-string v0, \"mobi.oneway.export.AdShowActivity\"")) {
					tmp = "    const-string v0, \"com.qy.AdShowActivity\"";
					write = true;
				}
				str += tmp + "\n";
			}
			reader.close();
			if (write) {
				FileWriter writer = new FileWriter(file);
				writer.write(str);
				writer.flush();
				writer.close();
				return;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			new TextToDalog("错误", "处理unit.smali文件失败：" + e.getMessage());
			return;
		}
		new TextToDalog("警告", "Unit.smali文件没有包含指定代码，请手动检查");
	}

	private void updateProvider(String path) {
		try {
			List<String> list = new ArrayList<>();
			File file = new File(path);
			BufferedReader reader = new BufferedReader(new FileReader(file));

			String tmp, str = "";
			boolean write = false;
			int index = 0;
			while ((tmp = reader.readLine()) != null) {
				list.add(tmp);
				if (tmp.trim().equals("const-string v1, \"mobi.oneway.export.OWProvider\"")) {
					if (list.get(index - 1).trim().equals(":try_start_0")
							&& list.get(index - 3).trim().equals("const/4 v0, 0x0")) {
						list.set(index - 3, "    const/4 v0, 0x1");
						write = true;
					}
				}
				index++;
			}
			for (String string : list) {
				str += string + "\n";
			}
			reader.close();

			if (write) {
				FileWriter writer = new FileWriter(file);
				writer.write(str);
				writer.flush();
				writer.close();
				return;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			new TextToDalog("错误", "处理c.smali文件失败：" + e.getMessage());
			return;
		}
		new TextToDalog("警告", "c.smali文件没有包含指定代码，请手动检查");
	}
}
