package com.danding.yang800.Presenter;

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

import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.MainActivity;
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 MainActivityPresenter {
    private MainActivity context;
    SharedPreferences sp;

    public MainActivityPresenter(MainActivity 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);
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }


            }

            @Override
            public void onerrSuccess() {
            }

            @Override
            public void onCatch() {
            }

            @Override
            public void onFail() {
            }
        });
    }

    public void getInfo(final String storeId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        new VolleyUtil(context).request(context, "finance.storeIncome.getStoreIncome", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString() + "今日订单");
                try {
                    JSONObject jsonData = json.getJSONObject("result");
                    String storeTotalIncome = StringUtil.doubleToString(jsonData, "storeTotalIncome");
                    String storeTodayIncome = StringUtil.doubleToString(jsonData, "storeTodayIncome");
                    context.updateProfit(storeTotalIncome, storeTodayIncome);
                    LogUtil.d(storeTodayIncome + "今日订单");
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {
            }
        });

    }

    public void getShopInfo(final String id) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        new VolleyUtil(context).request(context, "store.setting.update.getStoreInfo", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject jsondata = json
                            .getJSONObject("result");
                    String name = jsondata.getString("name");
                    String headImg = jsondata.getString("avatar");
                    context.updateShopHead(name, headImg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {
            }
        });
//
    }

    public void accessLogCount(final String id) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", id);
        new VolleyUtil(context).request(context, "store.accessLogCount", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString() + "7584555");
                try {
                    String data = json
                            .getString("result");
                    context.updateaccessLogCount(data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {
                LogUtil.d("fail");
            }
        });
//
    }

    public void orderOfToday(final String id) {
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", id);
        new VolleyUtil(context).request(context, "order.orderOfToday", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString());
                try {
                    String data = json
                            .getString("result");
                    context.updateorderOfToday(data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {
            }
        });
//
    }


    public void gethtmlUrl() {
        HashMap<String, String> map = new HashMap<>();
        new VolleyUtil(context).request(context, "login.htmlUrl", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                LogUtil.d(json.toString()+"登录链接");
                try {
                    String result=json.getString("result");
                    String htmlUrl=result+"?"+"referralCode="+sp.getString("referralCode","");
                    sp.edit().putString("htmlUrl",htmlUrl).commit();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onerrSuccess() {

            }

            @Override
            public void onCatch() {

            }

            @Override
            public void onFail() {
            }
        });
//
    }

}
