package com.danding.yang800.Presenter;

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

import com.danding.yang800.Adapter.InviteNoteAdapter;
import com.danding.yang800.Entity.InviteNoteInfo;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.InviteNoteActivity;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.VolleyUtil;
import com.danding.yang800.Vo.GiftInfoVO;
import com.danding.yang800.Vo.InviteNoteVO;
import com.google.gson.Gson;

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

import java.util.HashMap;
import java.util.List;

public class InviteNoteActivityPresenter {
    private InviteNoteActivity context;
    InviteNoteAdapter adapter;

    public InviteNoteActivityPresenter(InviteNoteActivity context, InviteNoteAdapter adapter) {
        this.context = context;
        this.adapter = adapter;
    }


    public void getInfo() {

        HashMap<String, String> map = new HashMap<>();

        new VolleyUtil(context).request(context, "store.inviteChildrenStore", true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    Gson gson = new Gson();
                    InviteNoteVO bean = gson.fromJson(json.toString(), InviteNoteVO.class);
                    InviteNoteVO.ResultBean result = bean.getResult();
                    List<InviteNoteVO.ResultBean.ChildListBean> childList = result.getChildList();
                    for (int i = 0; i < childList.size(); i++) {
                        String name = childList.get(i).getName();
                        String mobile = childList.get(i).getMobile();
                        String registerTime = childList.get(i).getRegisterTime();
                        adapter.addData(new InviteNoteInfo(name, mobile, registerTime));
                    }
                    int childCount = result.getChildCount();
                    String inviteImgUrl = result.getInviteImgUrl();
//                    JSONObject jsonData = json.getJSONObject("result");
//                    JSONArray childList = jsonData.getJSONArray("childList");
//                    int count = childList.length();
//                    for (int i = 0; i < childList.length(); i++) {
//                        JSONObject obj = childList.getJSONObject(i);
//                        String name = obj.getString("name");
//                        String number = obj.getString("mobile");
//                        String time = StringUtil.getStrTime(obj.getLong("createTime") / 1000 + "");
//                        adapter.addData(new InviteNoteInfo(name, number, time));
//                    }
                    context.initData(childCount, inviteImgUrl);
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();

                }
            }


            @Override
            public void onerrSuccess() {
            }

            @Override
            public void onCatch() {
            }

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


}
