package com.danding.yang800.Presenter;

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

import com.danding.yang800.BuildConfig;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.General.Yang800App;
import com.danding.yang800.TakeToAliActivity;
import com.danding.yang800.Util.GeneralUtil;
import com.danding.yang800.Util.MD5Util;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by Administrator on 2017/1/12.
 */
public class TakeToAliActivityPresenter {
    private TakeToAliActivity context;
    SharedPreferences sp;

    public TakeToAliActivityPresenter(TakeToAliActivity context) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
    }

    public void apply(final String storeId, final String userId, final String withdrawCashPassword,
                      final String applyMoney, final String accountInfo) {
        if (withdrawCashPassword.equals("") || applyMoney.equals("")) {
            ToastUtil.show(context, "提现金额或提现密码不能为空！");
            return;
        }
        final String withdrawCashPasswordEncode = GeneralUtil.encode(MD5Util.MD5(withdrawCashPassword) + "yang800_$_&_%_#_@");
        context.initDialog();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("storeId", storeId);
        map.put("userId", userId);
        map.put("withdrawCashPassword", withdrawCashPasswordEncode);
        map.put("applyMoney", applyMoney);
        map.put("accountInfo", accountInfo);
//        new VolleyUtil(context).request( context,"http://" + BuildConfig.HOSTURL + "/xhr/finance/storeIncome/cashWithdrawApply",true, map, new HttpCallBack() {
        new VolleyUtil(context).request( context, "finance.storeIncome.cashWithdrawApply",true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                        ToastUtil.show(context, "申请提现成功");
                        context.applySuccess();
                    context.dismissDialog();
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();

                }

            }


            @Override
            public void onerrSuccess() {
                context.dismissDialog();
            }

            @Override
            public void onCatch() {
                context.dismissDialog();
            }

            @Override
            public void onFail() {
                context.dismissDialog();
            }
        });
//        StringRequest stringRequest = new StringRequest(Request.Method.POST,
//                "http://" + BuildConfig.HOSTURL + "/xhr/finance/storeIncome/cashWithdrawApply",
//                new Response.Listener<String>() {
//                    @Override
//                    public void onResponse(String response) {
//                        Log.d("LoginActivity", response);
//                        //System.out.println(response);
//                        try {
//                            JSONObject json = new JSONObject(response);
//                            String code = json.getString("code");
//                            if (code.equals("success")) {
//                                ToastUtil.show(context, "申请提现成功");
//                                context.applySuccess();
//                            } else {
//                                String errorMsg = json.getString("errorMsg");
//                                ToastUtil.show(context, errorMsg);
//                            }
//                            context.dismissDialog();
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                            context.dismissDialog();
//                        }
//
//                    }
//                }, new Response.ErrorListener() {
//
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                context.dismissDialog();
//            }
//        }) {
//            @Override
//            protected HashMap<String, String> getParams() { //
//                HashMap<String, String> map = new HashMap<String, String>();
//                map.put("storeId", storeId);
//                map.put("userId", userId);
//                map.put("withdrawCashPassword", withdrawCashPasswordEncode);
//                map.put("applyMoney", applyMoney);
//                map.put("accountInfo", accountInfo);
//                return map;
//            }
//
//
//        };
//        stringRequest.setTag("POST_TAG");
//        requestQueue.add(stringRequest);
    }


}
