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.MyProfitActivity;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONObject;

import java.util.HashMap;

public class MyProfitActivityPresenter {
    private MyProfitActivity context;
    SharedPreferences sp;

    public MyProfitActivityPresenter(MyProfitActivity context) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
    }
    public void balance(final String storeId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        new VolleyUtil(context).request(context,  "finance.storeIncome.balance",true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    String balance = StringUtil.doubleToString(json, "result");
                    context.updateBalance(balance);
                    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 cashWithdrawApplySum(final String storeId) {
//        HashMap<String, String> map = new HashMap<>();
//        map.put("storeId", storeId);
//
//        new VolleyUtil(context).request(context, "finance.storeIncome.cashWithdrawApplySum",true, map, new HttpCallBack() {
//            @Override
//            public void onSuccess(JSONObject json) {
//                try {
//                    LogUtil.d(json.toString());
//                    String balance = StringUtil.doubleToString(json, "result");
//                    context.updateTake(balance);
//                    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 getWaitingSettlementIncome(final String storeId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);

        new VolleyUtil(context).request(context, "finance.storeIncome.getWaitingSettlementIncome",true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    LogUtil.d(json.toString()+"获取待结算收益");
                    String profit = StringUtil.doubleToString(json, "result");
                    context.updateWaitProfit(profit);
                } 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 getInfo(final String userId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("userId", userId);
//        new VolleyUtil(context).request(context, "http://" + BuildConfig.HOSTURL + "/xhr/user/setting/toUpdateUserInfo",true, map, new HttpCallBack() {
        new VolleyUtil(context).request(context, "user.setting.toUpdateUserInfo", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject jsonObject = json.getJSONObject("result");
                    boolean realNameSet = false;
                    String realname = "";
                    try {
                        realNameSet = jsonObject.getBoolean("realNameSet");
                        realname = jsonObject.getString("realname");
                    } catch (Exception e) {

                    }

                    context.initData(realNameSet,realname);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }


            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {

            }
        });
    }
}
