package com.yyrh.pay;


import android.os.Handler;
import android.os.Message;

import com.yyrh.networking.urlhttp.CallBackUtil;
import com.yyrh.networking.urlhttp.UrlHttpUtil;
import com.yyrh.sdk.SDKEntry;
import com.yyrh.utils.YYLog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;

public class PaymentNotify extends Handler {
    private static final int MAX_TIME = 5;

    @Override
    public void handleMessage(final Message msg) {
        super.handleMessage(msg);
        final HashMap<String, String> payInfo = (HashMap<String, String>) msg.obj;
        String url = payInfo.get("url");
       // url = "http://testmixplatformapi.yyxxgame.com:9003/client_test";
        int times = 0;

        if (payInfo.containsKey("resend_times")) {
            times = Integer.valueOf(payInfo.get("resend_times")).intValue();
            payInfo.remove("resend_times");
        }

        final int finalTimes = times;
        UrlHttpUtil.post(url, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                if (finalTimes < MAX_TIME) {
                    payInfo.put("resend_times", finalTimes + 1 + "");
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = payInfo;

                    YYLog.i( "PaymentNotify-payInfo: NetWork Error" + payInfo.toString());

                    switch (finalTimes) {

                        case 0:
                            sendMessageDelayed(msg, 10000);
                            break;
                        case 1:
                            sendMessageDelayed(msg, 30000);
                            break;
                        case 2:
                            sendMessageDelayed(msg, 60000);
                            break;
                        case 3:
                            sendMessageDelayed(msg, 180000);
                            break;
                        case 4:
                            sendMessageDelayed(msg, 300000);
                            break;

                    }
                }
            }

            @Override
            public void onResponse(String response) {
                JSONObject json = null;

                try {
                    json = new JSONObject(response);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int code = json.optInt("code",-1);
                if (code != 1 && finalTimes < MAX_TIME){
                    payInfo.put("resend_times", finalTimes + 1 + "");
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = payInfo;

                    YYLog.i( "PaymentNotify-payInfo:" + payInfo.toString());
                    switch (finalTimes){
                        case 0:

                            sendMessageDelayed(msg, 10000);
                            break;
                        case 1:

                            sendMessageDelayed(msg, 30000);
                            break;
                        case 2:

                            sendMessageDelayed(msg, 60000);
                            break;
                        case 3:

                            sendMessageDelayed(msg, 180000);
                            break;
                        case 4:

                            sendMessageDelayed(msg, 300000);
                            break;

                    }

                }else{

                    SDKEntry.getSdkInstance().sdkCallback.onPayFinishCallback(true,"pay success");

                }
            }
        });
    }
}
