package com.danding.yang800.Presenter;

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

import com.danding.yang800.AccountDetailedActivity;
import com.danding.yang800.Adapter.AccountDetailedAdapter;
import com.danding.yang800.BuildConfig;
import com.danding.yang800.Entity.AccountDetailedInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.VolleyUtil;

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

import java.util.HashMap;

public class AccountDetailedActivityPresenter {
    private AccountDetailedActivity context;
    SharedPreferences sp;
    AccountDetailedAdapter adapter;

    public AccountDetailedActivityPresenter(AccountDetailedActivity context, AccountDetailedAdapter adapter) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
        this.adapter = adapter;
    }

    public void accountDetail(final String storeId, final String pageIndex) {
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        map.put("pageIndex", pageIndex);
        map.put("limit", "10");
        new VolleyUtil(context).request(context, "finance.storeIncome.accountDetail", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    JSONObject jsonOData = json.getJSONObject("result");
                    int totalCount = jsonOData.getInt("pageTotal");
                    JSONArray jjArray = jsonOData
                            .getJSONArray("data");
                    for (int i = 0; i < jjArray.length(); i++) {
                        JSONObject jsondata = jjArray.getJSONObject(i);
                        String remark = jsondata.getString("remark");
                        String createTime = jsondata.getString("createTime");
                        String packageSn = StringUtil.toString(jsondata.getString("packageSn"));
                        String addMoney = StringUtil.doubleToString(jsondata, "addMoney");
                        adapter.addData(new AccountDetailedInfo(createTime, addMoney, remark,packageSn));
                    }
                    context.upadateAdapter(totalCount);
                    context.dismissDialog();
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }
            }

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

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

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

    }


}
