package com.danding.yang800.Presenter;

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

import com.danding.yang800.BankInfoActivity;
import com.danding.yang800.BuildConfig;
import com.danding.yang800.Entity.AddressInfo;
import com.danding.yang800.Entity.BankInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.Util.GeneralUtil;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.VolleyUtil;

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

import java.util.ArrayList;
import java.util.HashMap;


public class BankInfoActivityPresenter {
    private BankInfoActivity context;
    SharedPreferences sp;
    ArrayList<AddressInfo> provinceList = new ArrayList<>();
    ArrayList<AddressInfo> cityList = new ArrayList<>();
    ArrayList<AddressInfo> regionList = new ArrayList<>();

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

    public void getAreaInfo(final String type) {
        HashMap<String, String> map = new HashMap<>();
        map.put("type", type);

        new VolleyUtil(context).request(context, "area.getByType", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                //System.out.println(response);
                try {
                    JSONArray jsonArray = json.getJSONArray("result");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObj = jsonArray.getJSONObject(i);
                        String id = jsonObj.getString("id");
                        String name = jsonObj.getString("name");
                        String parentId = jsonObj.getString("parentId");
                        provinceList.add(new AddressInfo(id, name, parentId));
                    }
                    context.updateProvince(provinceList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onerrSuccess() {
            }

            @Override
            public void onCatch() {

            }

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

    public void getChildren(final String parentId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("parentId", parentId);
        new VolleyUtil(context).request(context, "area.getChildren", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    cityList.clear();
                    JSONArray jsonArray = json.getJSONArray("result");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObj = jsonArray.getJSONObject(i);
                        String id = jsonObj.getString("id");
                        String name = jsonObj.getString("name");
                        String parentId = jsonObj.getString("parentId");
                        cityList.add(new AddressInfo(id, name, parentId));

                    }
                    context.updateCity(cityList);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

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

    public void getRegionChildren(final String parentId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("parentId", parentId);
        new VolleyUtil(context).request(context, "area.getChildren", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    regionList.clear();
                    JSONArray jsonArray = json.getJSONArray("result");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObj = jsonArray.getJSONObject(i);
                        String id = jsonObj.getString("id");
                        String name = jsonObj.getString("name");
                        String parentId = jsonObj.getString("parentId");
                        regionList.add(new AddressInfo(id, name, parentId));

                    }
                    context.updateRegion(regionList);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

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

    public void getInfo(final String storeId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("cashAccountType", "1");
        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 userName = jsonInfo.getString("userName");
                        String subBranch = jsonInfo.getString("subBranch");
                        String bankCardNum = jsonInfo.getString("bankCardNum");
                        String identity = jsonInfo.getString("identity");
                        String weChat = jsonInfo.getString("weChat");
                        JSONObject jsonAddress = jsonInfo.getJSONObject("address");
                        JSONObject jsonprovice = jsonAddress.getJSONObject("provice");
                        String provinceId = jsonprovice.getString("id");
                        String provinceName = jsonprovice.getString("name");
                        JSONObject jsoncity = jsonAddress.getJSONObject("city");
                        String cityId = jsoncity.getString("id");
                        String cityName = jsoncity.getString("name");
                        JSONObject jsondistrict = jsonAddress.getJSONObject("district");
                        String districtId = jsondistrict.getString("id");
                        String districtName = jsondistrict.getString("name");
                        JSONObject jsonbank = jsonInfo.getJSONObject("bank");
                        String bankCode = jsonbank.getString("code");
                        String bankName = jsonbank.getString("name");
                        context.initData(userName, subBranch, bankCardNum, identity, weChat, provinceId, provinceName, cityId
                                , cityName, districtId, districtName, bankCode, bankName);
                    }
                    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) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("accountType", "1");
        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();
                    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();
            }
        });
    }

    ArrayList<BankInfo> bankInfoArrayList = new ArrayList<>();

    public void getBank() {
        HashMap<String, String> map = new HashMap<>();
        new VolleyUtil(context).request(context, "finance.storeIncome.getBank", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONArray jsonArraydata = json.getJSONArray("result");
                    for (int i = 0; i < jsonArraydata.length(); i++) {
                        JSONObject jsonObj = jsonArraydata.getJSONObject(i);
                        String name = jsonObj.getString("name");
                        String bankCode = jsonObj.getString("code");
                        bankInfoArrayList.add(new BankInfo(name, bankCode));
                    }
                    context.initBank(bankInfoArrayList);
                    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();
            }
        });
    }


}
