package com.wisewoods.eduandroid.ui;

import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.wisewoods.base.bean.BaseEvent;
import com.wisewoods.base.bean.TeacherCourseList;
import com.wisewoods.base.net.JCallback;
import com.wisewoods.base.page.BaseListActivity;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityTeacherCourseBinding;
import com.wisewoods.eduandroid.ui.adapter.CourseAdapter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import retrofit2.Call;
import retrofit2.Response;

public class TeacherCourseActivity extends BaseListActivity<ActivityTeacherCourseBinding>
        implements OnItemClickListener {

    private CourseAdapter mAdapter;

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(BaseEvent.TeacherCourseRefreshEvent event) {
        onRefresh();
    }

    @Override
    protected void initListData() {
        srf = binding.srf;
        rv = binding.rvTeacherCourse;

        showBarBack(binding.bar.tvTitle, "课程管理", binding.bar.ivBack);

        binding.tvAdd.setOnClickListener(v -> AddCourseActivity.start("添加课程", null));

        mAdapter = new CourseAdapter();
        mAdapter.setOnItemClickListener(this);
    }

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

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

                    }

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

    @Override
    public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter,
                            @NonNull @NotNull View view, int position) {
        TeacherCourseList.DataBean item = mAdapter.getItem(position);
        if (item.auditStatus == 1) {
            AddCourseActivity.start("编辑课程", item);
        } else if (item.auditStatus == 2) {
            AddCourseActivity.start("编辑课程", item);
        }
    }
}
