package com.gh.common.util;

import android.content.Context;
import android.telephony.TelephonyManager;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.gh.base.AppController;
import com.gh.gamecenter.volley.extended.JsonArrayExtendedRequest;
import com.gh.gamecenter.volley.extended.StringExtendedRequest;

import org.json.JSONArray;

import java.util.UUID;

/**
 * Created by khy on 2016/8/24.
 */
public class ConcernUtils {

    public static void loadConcernData(final String url, final DownJsonListener listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                JsonArrayExtendedRequest request = new JsonArrayExtendedRequest(url,
                        new Response.Listener<JSONArray>() {
                            @Override
                            public void onResponse(JSONArray response) {
                                if (listener != null){
                                    listener.downSucced(response.toString());
                                }
                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                if (listener != null){
                                    listener.downFailed();
                                }
                            }
                        });
                request.setShouldCache(false);
                AppController.addToRequestQueue(request);
            }
        }).start();

    }

    public static void postConcernGameId(final String gameId, final String postUrl, final DownJsonListener listener){
        new Thread(new Runnable() {
            @Override
            public void run() {

                JSONArray data = new JSONArray();
                data.put(gameId);

                StringExtendedRequest request = new StringExtendedRequest(
                        Request.Method.POST, postUrl, data.toString(),
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                if (listener != null) {
                                    listener.downSucced("关注成功");
                                }
                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                if (listener != null) {
                                    listener.downFailed();
                                }
                            }
                        });
                request.setShouldCache(false);
                AppController.addToRequestQueue(request);
            }
        }).start();
    }

    public static void deleteConcernData(final String url, final DownJsonListener listener){
        new Thread(new Runnable() {
            @Override
            public void run() {
                StringExtendedRequest request = new StringExtendedRequest(
                        Request.Method.DELETE, url,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                if (listener != null) {
                                    listener.downSucced("删除成功");
                                }
                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                if (listener != null) {
                                    listener.downFailed();
                                }
                            }
                        });
                request.setShouldCache(false);
                AppController.addToRequestQueue(request);
            }
        }).start();
    }

    public static void updateConcernData(final String url, final JSONArray data, final DownJsonListener listener){
        new Thread(new Runnable() {
            @Override
            public void run() {
                StringExtendedRequest request = new StringExtendedRequest(
                        Request.Method.PUT, url, data.toString(),
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                if (listener != null) {
                                    listener.downSucced("更新设备关注成功");
                                }
                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                if (listener != null) {
                                    listener.downFailed();
                                }
                            }
                        });
                request.setShouldCache(false);
                AppController.addToRequestQueue(request);
            }

        }).start();
    }

    public interface DownJsonListener {
        void downSucced(String str);
        void downFailed();
    }

    // 获取设备号ID
    public static String uuid(Context context){
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        final String tmDevice, tmSerial, androidId;
        tmDevice = "" + tm.getDeviceId();
        tmSerial = "" + tm.getSimSerialNumber();
        androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

        UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
        return deviceUuid.toString();
    }

}

