package com.wisewoods.eduandroid.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wisewoods.base.app.UserManager;
import com.wisewoods.base.bean.Role;
import com.wisewoods.base.bean.StudentList;
import com.wisewoods.base.net.JCallback;
import com.wisewoods.base.page.BaseListActivity;
import com.wisewoods.base.utils.$;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityStudentBinding;
import com.wisewoods.eduandroid.ui.adapter.StudentAdapter;

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

import retrofit2.Call;
import retrofit2.Response;

public class StudentActivity extends BaseListActivity<ActivityStudentBinding> {

    private String name;
    private StudentAdapter mAdapter;
    private String id;
    private String teamId;
    private String scheduleId;
    private boolean isReport;
    private List<String> checks;
    private List<String> reports;
    private List<String> onlines;
    private String checkIndex = "";
    private String reportIndex = "";
    private String onlineIndex = "";

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

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

    @SuppressLint("MissingPermission")
    @Override
    protected void initListData() {
        rv = binding.rvStudent;
        srf = binding.srf;
        id = getIntent().getStringExtra("id");
        teamId = getIntent().getStringExtra("teamId");
        scheduleId = getIntent().getStringExtra("scheduleId");
        isReport = getIntent().getBooleanExtra("isReport", false);

        showBarBack(binding.bar.tvTitle, "学员列表", binding.bar.ivBack);

        checks = new ArrayList<>();
        checks.add("全部");
        checks.add("已签到");
        checks.add("未签到");
        checks.add("迟到");
        checks.add("请假");
        checks.add("旷课");

        reports = new ArrayList<>();
        reports.add("全部");
        reports.add("未报到");
        reports.add("已报到");

        onlines = new ArrayList<>();
        onlines.add("全部");
        onlines.add("线下");
        onlines.add("线上");

        binding.tvCheck.setOnClickListener(v -> {
            KeyboardUtils.hideSoftInput(this);

            OptionsPickerView pvOptions = new OptionsPickerBuilder(activity, (options1, option2, options3, v1) -> {
                //返回的分别是三个级别的选中位置
                String tx = checks.get(options1);
                if (options1 == 0) {
                    checkIndex = "";
                } else {
                    checkIndex = (options1) + "";
                }
                binding.tvCheck.setText(tx);
                onRefresh();
            }).build();
            pvOptions.setPicker(checks);
            pvOptions.show();
        });
        binding.tvClass.setOnClickListener(v -> {
            KeyboardUtils.hideSoftInput(this);

            OptionsPickerView pvOptions = new OptionsPickerBuilder(activity, (options1, option2, options3, v1) -> {
                //返回的分别是三个级别的选中位置
                String tx = reports.get(options1);
                if (options1 == 0) {
                    reportIndex = "";
                } else {
                    reportIndex = (options1 - 1) + "";
                }
                binding.tvClass.setText(tx);
                onRefresh();
            }).build();
            pvOptions.setPicker(reports);
            pvOptions.show();
        });
        binding.tvOnline.setOnClickListener(v -> {
            KeyboardUtils.hideSoftInput(this);

            OptionsPickerView pvOptions = new OptionsPickerBuilder(activity, (options1, option2, options3, v1) -> {
                //返回的分别是三个级别的选中位置
                String tx = onlines.get(options1);
                if (options1 == 0) {
                    onlineIndex = "";
                } else {
                    onlineIndex = (options1 - 1) + "";
                }
                binding.tvOnline.setText(tx);
                onRefresh();
            }).build();
            pvOptions.setPicker(onlines);
            pvOptions.show();
        });
        binding.etStudent.setOnEditorActionListener((v, actionId, event) -> {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String name = binding.etStudent.getText().toString().trim();
                this.name = name;
                onRefresh();
            }
            return handled;
        });

        mAdapter = new StudentAdapter(isReport);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.ll_sleep:
                    SleepActivity.start(mAdapter.getItem(position));
                    break;
                case R.id.tv_leave:
                    StudentLeaveActivity.start(mAdapter.getItem(position).resourceStuId + "");
                    break;
                case R.id.tv_phone:
                    $.requestPhone(this,()->{
                        PhoneUtils.call(mAdapter.getItem(position).phone);
                    });
                    break;
            }
        });

        initUI();
    }

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

    @Override
    protected void getData(long page, long size) {
        if (TextUtils.isEmpty(scheduleId)) {
            App.api.getStudentList(id, name, reportIndex,
                    page, size)
                    .enqueue(new JCallback<StudentList>() {
                        @Override
                        public void onS(Response<StudentList> response, StudentList studentList) {
                            getDataSuccess(studentList.data.rows, studentList.data.total);
                        }

                        @Override
                        public void onFailure(Call<StudentList> call, Throwable t) {
                            super.onFailure(call, t);
                            getDataFail();
                        }
                    });
        } else {
            App.api.getScheduleStudent(scheduleId, name, checkIndex, onlineIndex,
                    page, size)
                    .enqueue(new JCallback<StudentList>() {
                        @Override
                        public void onS(Response<StudentList> response, StudentList studentList) {
                            getDataSuccess(studentList.data.rows, studentList.data.total);
                        }

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


    }

    private void initUI() {
        Role role = UserManager.getUser().getRole();
        switch (role) {
            case ORGANIZER:
            case UNDERTAKER:
                binding.llOpt.setVisibility(View.GONE);
                break;
            case EDUCATION:
                binding.llOpt.setVisibility(View.GONE);
                break;
            case TRAIN:
                binding.llOpt.setVisibility(View.VISIBLE);
                if (isReport) {
                    binding.flCheck.setVisibility(View.GONE);
                    binding.flClass.setVisibility(View.VISIBLE);
                    binding.flOnline.setVisibility(View.GONE);
                } else {
                    binding.flCheck.setVisibility(View.VISIBLE);
                    binding.flClass.setVisibility(View.GONE);
                    binding.flOnline.setVisibility(View.VISIBLE);
                }
                break;
            case TEACHER:
                binding.llOpt.setVisibility(View.GONE);
                break;
        }
    }
}
