package com.wisewoods.eduandroid.ui;

import android.content.Intent;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wisewoods.base.base.BaseVH;
import com.wisewoods.base.bean.ClassInfo;
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.base.utils.$;
import com.wisewoods.base.utils.DialogUtils;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityClassBinding;
import com.wisewoods.eduandroid.ui.adapter.ClassAdapter;

import java.util.HashMap;

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

public class ClassActivity extends BaseListActivity<ActivityClassBinding> {

    private String id;
    private ClassAdapter classAdapter;

    public static void start(String id) {
        Intent intent = new Intent(ActivityUtils.getTopActivity(), ClassActivity.class);
        intent.putExtra("id", id);
        ActivityUtils.startActivity(intent);
    }

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

        showBarBack(binding.bar.tvTitle, "班级列表", binding.bar.ivBack);
        id = getIntent().getStringExtra("id");

        classAdapter = new ClassAdapter();
        classAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.tv_student_loading:
                case R.id.tv_student_list:
                    StudentActivity.start(classAdapter.getItem(position).teamId + "", false);
                    break;
                case R.id.tv_check_detail:
                    StudentActivity.start(classAdapter.getItem(position).teamId + "", true);
                    break;
                case R.id.tv_res:
                    ResActivity.start(classAdapter.getItem(position).teamId + "", "0", "");
                    break;
                case R.id.tv_open:
                    DialogUtils.showCommon(
                            "确认" + classAdapter.getItem(position).teamName + "开启报到？", (DialogUtils.IDialogOnOKListener) dialog -> {
                                    openBD(classAdapter.getItem(position), position);
                            });
                    break;
                case R.id.tv_check:
                    ClassInfo.DataBean item = classAdapter.getItem(position);
                    String start = $.timeFormat2(item.teamStart);
                    String end = $.timeFormat2(item.teamEnd);
                    ScheduleActivity.start(item.teamId + "", start, end);
//                    if (!TextUtils.isEmpty(item.teamGroupId) && !TextUtils.isEmpty(item.teamGroupName)){
//
//                        String[] ids = item.teamGroupId.split(",");
//                        String[] names = item.teamGroupName.split(",");
//
//                        if (ids.length > 1) {
//                            DialogUtils.showList("请选择班组", Arrays.asList(names),
//                                    (materialDialog, index, text) -> {
//                                        ScheduleActivity.start(ids[index],start,end);
//                                        return null;
//                                    });
//                        } else if (ids.length == 1) {
//                            ScheduleActivity.start(ids[0],start,end);
//                        } else {
//                            $.toast("无信息");
//                        }
//                    }else {
//                        $.toast("无信息");
//                    }

                    break;
            }
        });
    }


    private void openBD(ClassInfo.DataBean item, int position) {
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        HashMap<String, String> map = new HashMap<>();
        map.put("id", item.teamId + "");
        map.put("openReport", "1");

        RequestBody body = RequestBody.create(GsonUtils.toJson(map), JSON);
        App.api.openTeam(body)
                .enqueue(new JDCallback<Results>() {
                    @Override
                    public void onS(Response<Results> response, Results results) {
                        $.toast("开启成功");
                        item.openReport = 1;
                        classAdapter.notifyItemChanged(position);
                    }
                });
    }

    @Override
    public BaseQuickAdapter<ClassInfo.DataBean, BaseVH> getAdapter() {
        return classAdapter;
    }


//    @Override
//    public BaseQuickAdapter getAdapter() {
//        return classAdapter;
//    }


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

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