package com.stu.assistant.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.stu.assistant.R;
import com.stu.assistant.adapter.TaskAllListAdapter;
import com.stu.assistant.base.BaseActivity;
import com.stu.assistant.base.BaseApplion;
import com.stu.assistant.bean.TaskBean;
import com.stu.assistant.uitls.DividerItemDecorationUtil;
import com.stu.assistant.uitls.LogUtils;
import com.stu.assistant.uitls.SharedConfig;
import com.stu.assistant.uitls.SharedUtil;
import com.stu.assistant.view.MySwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
//作业列表
public class TaskListActivity extends BaseActivity {
    private RecyclerView mrv;
    private TaskAllListAdapter adapter;
    private MySwipeRefreshLayout swip;
    private Handler handler;
    private String userLevel, title;
    private int itemType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_table_list;
    }

    @Override
    protected void initView() {
        swip = findViewById(R.id.swip_rf);
        mrv = findViewById(R.id.mrv);
        handler = new Handler();
    }

    @Override
    protected void initData() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mrv.addItemDecoration(new DividerItemDecorationUtil(this, false));
        mrv.setLayoutManager(manager);
        Intent intent = getIntent();
        userLevel = intent.getStringExtra("userLevel");
        title = intent.getStringExtra("title");
        itemType = intent.getIntExtra("itemType", 0);
        setTitleLay(R.id.title_tv, R.id.back_lay, title);
        setTextMsg(R.id.btn_register, "添加" + title);
        getData();
    }

    @Override
    protected void initListener() {
        // 刷新数据
        swip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 这里的作用是防止下拉刷新的时候还可以上拉加载
                getData();
            }
        });
    }

    private void getData() {
        showProDialog("正在查询");
        BmobQuery<TaskBean> query = new BmobQuery<>();
        query.addWhereEqualTo("stuId", SharedUtil.create(BaseApplion.application).getString(SharedConfig.objectId));
        query.findObjects(new FindListener<TaskBean>() {
                    @Override
                    public void done(List<TaskBean> list, BmobException e) {
                        LogUtils.i("list", "-stuId-findObjects-->" + new Gson().toJson(list));
                        dismissProDialog();
                        setAdapter((ArrayList<TaskBean>) list);
                    }
                });
    }

    public void setAdapter(ArrayList<TaskBean> mList) {
        if (mList == null) {
            mList = new ArrayList<>();
        }
        setTitleLay(R.id.title_tv, R.id.back_lay, title + "(" + mList.size() + ")");
        if (adapter == null) {
            adapter = new TaskAllListAdapter(this, mList);
            mrv.setAdapter(adapter);
            adapter.setOnItemClickListeners(new TaskAllListAdapter.OnItemClickListeners() {
                @Override
                public void itemBean(int pos, TaskBean tableListBean) {
                    Intent intent = new Intent(TaskListActivity.this, AddTaskActivity.class);
                    intent.putExtra("bean",tableListBean);
                    startActivityForResult(intent, 201);
                }
            });
        } else {
            adapter.setList(mList);
            adapter.notifyDataSetChanged();

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    swip.setRefreshing(false);
                }
            }, 1000);
            if (mList.size() == 0) {
                Toast.makeText(this, "暂无数据", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    public void BtnAddOnClick(View view) {
        Intent intent = new Intent(TaskListActivity.this, AddTaskActivity.class);
        startActivityForResult(intent, 201);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 201) {
            if (resultCode == 12) {
                getData();
            }
        }
    }
}