package com.danding.yang800.Presenter;

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

import com.danding.yang800.BankInfoActivity;
import com.danding.yang800.Entity.AddressInfo;
import com.danding.yang800.Entity.BankInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.OrderPayActivity;
import com.danding.yang800.Util.GeneralUtil;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;
import com.danding.yang800.Vo.GiftInfoVO;
import com.google.gson.Gson;

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

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


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

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

    public void getInfo(String storeSkuSn) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeSkuSn", storeSkuSn);
        map.put("quantity", "1");
        new VolleyUtil(context).request(context, "order.giftBag.init", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    String result = StringUtil.toString(json.getString("result"));
                    context.initNumber(result);
                } catch (Exception e) {

                }

            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {

            }
        });
    }

    public void submit(String storeSkuSn, String provinceName, String cityName, String districtName, String address
            , String userName, String mobile) {
        if (provinceName.equals("") || cityName.equals("")) {
            ToastUtil.show(context, "省市区不能为空！");
            return;
        }
        if (address.equals("")) {
            ToastUtil.show(context, "详细地址不能为空！");
            return;
        }
        if (userName.equals("")) {
            ToastUtil.show(context, "收件人不能为空！");
            return;
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("storeSkuSn", storeSkuSn);
        map.put("quantity", "1");
        map.put("provinceName", provinceName);
        map.put("cityName", cityName);
        map.put("districtName", districtName);
        map.put("address", address);
        map.put("userName", userName);
        map.put("mobile", mobile);
        map.put("payWay", "ALIPAY");
        new VolleyUtil(context).request(context, "order.giftBag.submit", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    String result = StringUtil.toString(json.getString("result"));
                    context.aliPay(result);
                } catch (Exception e) {

                }

            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {

            }
        });
    }

    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() {

            }
        });
    }

    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() {

            }
        });
    }

    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() {

            }
        });
    }


}
