package com.wisewoods.eduandroid.ui;

import com.blankj.utilcode.util.GsonUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wisewoods.base.bean.MsgList;
import com.wisewoods.base.bean.Results;
import com.wisewoods.base.net.JCallback;
import com.wisewoods.base.net.JDCallback;
import com.wisewoods.base.page.BaseListActivity;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityMessageBinding;
import com.wisewoods.eduandroid.ui.adapter.MessageAdapter;

import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

public class MessageActivity extends BaseListActivity<ActivityMessageBinding> {

    private MessageAdapter messageAdapter;

    @Override
    protected void initListData() {
        showBarBack(binding.bar.tvTitle, "通知", binding.bar.ivBack);


        rv = binding.rvMessage;
        srf = binding.srf;
        messageAdapter = new MessageAdapter();
        messageAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tv_complete) {
                readMsg(messageAdapter.getItem(position), position);
            }
        });
    }

    private void readMsg(MsgList.MsgBean item, int position) {
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        HashMap<String, String> map = new HashMap<>();
        map.put("id", item.id + "");

        RequestBody body = RequestBody.create(GsonUtils.toJson(map), JSON);
        App.api.messageLog(body).enqueue(new JDCallback<Results>() {
            @Override
            public void onS(Response<Results> response, Results results) {
                item.readStatus = "1";
                messageAdapter.notifyItemChanged(position);
            }
        });
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return messageAdapter;
    }

    @Override
    protected void getData(long page, long size) {
        App.api.getMessageLog(page, size).enqueue(new JCallback<MsgList>() {
            @Override
            public void onS(Response<MsgList> response, MsgList msgList) {
                getDataSuccess(msgList.data.rows, msgList.data.total);
            }

            @Override
            public void onFailure(Call<MsgList> call, Throwable t) {
                super.onFailure(call, t);
                getDataFail();
            }
        });
    }
}
