package com.tagmae.tsgame_erwang;

import android.content.Context;
import android.util.Xml;

import com.android.volley.VolleyError;
import com.game.webgame.network.Volleyinterface;
import com.game.webgame.network.register;
import com.google.gson.Gson;
import com.tsgame.tsgame_niuniu.Bean.logbean;
import com.tsgame.tsgame_niuniu.Bean.sittingversion;
import com.tsgame.tsgame_niuniu.Bean.versionbean;
import com.tsgame.tsgame_niuniu.system.Myapplication;
import com.tsgame.tsgame_niuniu.util.Settingutil;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class commitLog {
	
	
	public static void commitLog(Context con, String msg, String Packet) {

		versionbean ver = null;
		try {
			ver = pullassetxml(con.getAssets().open(
					Settingutil.kfile + File.separator + "version.xml"));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		Map<String, String> map = new HashMap<String, String>();
		map.put("app", "youle");
		map.put("route", "agent");
		map.put("rpc", "submit_error");

		logbean date = new logbean();
		date.setAgentid(ver.getAgentid());
		date.setGameid(ver.getGameid());
		date.setMsg(msg);
		date.setPacket(Packet);
		date.setPlayerid("android");

		sittingversion sitting = new sittingversion();
		sitting.setApp("youle");
		sitting.setRoute("agent");
		sitting.setRpc("submit_error");
		sitting.setData(date);

		Gson gson = new Gson();
		String data = gson.toJson(sitting);

		System.out.println(data);
		// http://ylyxservice1.0791ts.cn?{"app":"youle","data":{"agentid":"00bA05haB0d9ZC0fwGD09Q2OA30insbQ","gameid":"8x4l0rGjf026f60c48h0mbUAhK5vV16f","msg":"ffffff","packet":"牛牛","playerid":"android"},"route":"agent","rpc":"submit_error"}
		register.PostJsonrequest1(data,
				"",
				new Volleyinterface(Myapplication.application,
						Volleyinterface.mlistener,
						Volleyinterface.mErrorLisener) {
					@Override
					public void onsuccess(String result) {
						System.out.println("result=" + result);
						Gson gson = new Gson();

					}

					@Override
					public void onerror(VolleyError arg0) {
						System.out.println("arg0=" + arg0);

					}
				});

	}

	private static versionbean pullassetxml(InputStream inputStream) {
		versionbean version2 = new versionbean();
		try {
			XmlPullParser parser = Xml.newPullParser();
			// 2.设置解析的文件流
			parser.setInput(inputStream, "utf-8");
			// 3.得到事件类型(START_DOCUMENT,END_DOCUMENT,START_TAG, END_TAG,
			// TEXT, etc.)
			int type = parser.getEventType();
			// 一直读到文档结尾
			while (type != XmlPullParser.END_DOCUMENT) {

				switch (type) {
				case XmlPullParser.START_TAG:
					// 得到标签的名字
					String tag_name = parser.getName();
					if ("agent".equals(tag_name)) {

						String agentid = parser.getAttributeValue(null, "id");
						System.out.println("agentid=" + agentid);
						version2.setAgentid(agentid);

					} else if ("game".equals(tag_name)) {
						String gameid = parser.getAttributeValue(null, "id");
						System.out.println("gameid=" + gameid);
						version2.setGameid(gameid);
					} else if ("version".equals(tag_name)) {

						String version = parser
								.getAttributeValue(null, "value");
						System.out.println("version=" + version);
						version2.setVersion(version);
					}

					break;
				default:
					break;

				}
				// 继续往下一个事件解析
				type = parser.next();
			}

		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return version2;
	}
}
