package com.example.mountaintravel.util;

import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.example.mountaintravel.kind.AddGraphic;
import com.example.mountaintravel.variable.Variable;
import com.smart.gtstrvl.BindActivity;
import com.smart.gtstrvl.EventInfoActivity;
import com.smart.gtstrvl.GameManagerActivity;
import com.smart.gtstrvl.MainActivity;
import com.smart.gtstrvl.MyGradeActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class EventinfoListener {
	private EventInfoActivity event;
	private SharedPreferences sp;

	public EventinfoListener(EventInfoActivity event) {
		this.event = event;
		sp = event.getSharedPreferences("variable", Context.MODE_PRIVATE);
	}

	public void pulldata(String parameter) {
		// TODO Auto-generated method stub

		HashMap<String, Object> parameterMap = StringChangeUtils.toObject(
				parameter, HashMap.class);
		// Object result_obj = parameterMap.get("repeaterAxisList");
		// List<String> AllUserInformation = (List<String>) result_obj;
		// for (int i = 0; i < AllUserInformation.size(); i++) {
		// HashMap<String, Object> userInfomation = StringChangeUtils
		// .toObject(AllUserInformation.get(i), HashMap.class);
		// String locationX = StringUtils.toString(userInfomation.get("axisX"));
		// String locationY=StringUtils.toString(userInfomation.get("axisY"));
		// //String repeaterSort =
		// StringUtils.toString(userInfomation.get("repeaterSort"));
		// //String userId = StringUtils.toString(userInfomation.get("axisX"));
		//
		//
		// }
		System.out.println(parameter);
		Object eventId2 = parameterMap.get("eventId");
		Object routeIdgame = parameterMap.get("routeId");
		int routeId = (Integer) routeIdgame;
		sp.edit().putInt("routeId", routeId).commit();
		Object isOrganizer2 = parameterMap.get("isOrganizer");
		Object eventType2 = parameterMap.get("eventType");
		Object eventStatus2 = parameterMap.get("eventStatus");
		Object eventStartTime2 = parameterMap.get("eventStartTime");
		Object eventEndTime2 = parameterMap.get("eventEndTime");
		// Object begintime = eventStartTime.substring(0, 10);
		Object realStartTime2 = parameterMap.get("realStartTime");
		Object deviceSnObj = parameterMap.get("deviceSn");
		String deviceSn = StringUtils.toString(deviceSnObj);
		// Object pointArr= parameterMap.get("repeaterAxisList");
		sp.edit().putString("deviceSn", deviceSn).commit();
		// String realtime = realStartTime.substring(0, 10);
		int eventId3 = (Integer) eventId2;
		String eventId1 = String.valueOf(eventId3);
		boolean isOrganizer3 = (Boolean) isOrganizer2;
		String isOrganizer = String.valueOf(isOrganizer3);
		int eventType3 = (Integer) eventType2;
		String eventType = String.valueOf(eventType3);
		int eventStatus3 = (Integer) eventStatus2;
		String eventStatus = String.valueOf(eventStatus3);
		long eventStartTime3 = (Long) eventStartTime2;
		long eventEndTime3 = (Long) eventEndTime2;
		String eventStartTime = String.valueOf(eventStartTime3);
		String eventEndTime = String.valueOf(eventEndTime3);
		// Object begintime = eventStartTime.substring(0, 10);
		String begintime = eventStartTime.substring(0, 10);
		String endtime = eventEndTime.substring(0, 10);
		String hourminfull = "";
		String hourmin = "";
		String hour = "";
		String min = "";
		String ss = "";
		String ifBegin = "";
		boolean manager = false;
		boolean official = false;

		if (eventType.equals("1") || eventType.equals("0")) {

			manager = false;
			official = false;// 正常为true
		}
		if (eventType.equals("2") && isOrganizer.equals("true")) {
			manager = true;
			official = false;
		}
		if (eventType.equals("2") && isOrganizer.equals("false")) {
			manager = false;
			official = false;
		}
		if (official) {
			hourminfull = Variable.getStrTime2(begintime);
			hourmin = hourminfull.substring(8, 12);
			hour = hourmin.substring(0, 2);
			min = hourmin.substring(2, 4);
			ss = hourmin.substring(4);
			if (eventStatus.equals("0")) {
				ifBegin = "未开始";
			}
			if (eventStatus.equals("1")) {
				ifBegin = "已开始";
			}
		} else if (!official) {
			if (eventStatus.equals("0")) {
				ifBegin = "未开始";
				hour = "00";
				min = "00";
				ss = "00";
			}
			if (eventStatus.equals("2")) {
				Intent intent = new Intent(event, MyGradeActivity.class);
				intent.putExtra("eventId", eventId1);

				event.startActivity(intent);
				return;
			}
			if (eventStatus.equals("1")) {
				if (realStartTime2 != null) {
					long realStartTime1 = (Long) realStartTime2;
					String realStartTime4 = String.valueOf(realStartTime1);
					String realtime = realStartTime4.substring(0, 10);

					ifBegin = "已开始";
					hourminfull = Variable.getStrTime2(realtime);
					hourmin = hourminfull.substring(8, 14);
					hour = hourmin.substring(0, 2);
					min = hourmin.substring(2, 4);
					ss = hourmin.substring(4);
				}

			}

		}
		sp.edit().putBoolean("eventinfo", true).commit();
		Intent intent = new Intent(event, GameManagerActivity.class);
		intent.putExtra("official", official);
		intent.putExtra("manager", manager);
		intent.putExtra("hour", hour);
		intent.putExtra("min", min);
		intent.putExtra("ifBegin", ifBegin);
		intent.putExtra("begintime", begintime);
		intent.putExtra("eventId", eventId1);
		intent.putExtra("eventStartTime", eventStartTime);
		intent.putExtra("eventEndTime", eventEndTime);
		intent.putExtra("ss", ss);
		if (eventType.equals("1") || eventType.equals("0")) {
			sp.edit().putString("parameter", parameter).commit();
		}
		event.startActivityForResult(intent, 2);
	}
}
