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.InputAliAccountActivity;
import com.danding.yang800.Util.GeneralUtil;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONObject;

import java.util.HashMap;


public class InputAliAccountActivityPresenter {
    private InputAliAccountActivity context;
    SharedPreferences sp;

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

    public void getInfo(final String storeId) {
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("cashAccountType", "2");
        new VolleyUtil(context).request(context,  "finance.storeIncome.getCashAccount",true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject jsonData = json.getJSONObject("result");
                    String infoEncode = jsonData.getString("info");
                    if (!infoEncode.equals("")) {
                        String infoDecode = GeneralUtil.decode(infoEncode);
                        JSONObject jsonInfo = new JSONObject(infoDecode);
                        String name = jsonInfo.getString("aliName");
                        String number = jsonInfo.getString("aliAccount");
                        String againNumber = jsonInfo.getString("aliAccountAgain");
                        context.initData(name, number, againNumber);
                    }
                    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();
            }
        });
    }

    public void editInfo(final String storeId, final String accountInfo) {
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("accountType", "2");
        map.put("accountInfo", accountInfo);
        new VolleyUtil(context).request(context, "finance.storeIncome.editCashAccount", true,map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    context.editSuccess(accountInfo);
                    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/editCashAccount",
//                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")) {
//                                context.editSuccess(accountInfo);
//                            }
//                            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("accountType", "2");
//                map.put("accountInfo", accountInfo);
//                return map;
//            }
//        };
//        stringRequest.setTag("POST_TAG");
//        requestQueue.add(stringRequest);
    }


}
