package com.stu.assistant.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.stu.assistant.R;
import com.stu.assistant.base.BaseActivity;
import com.stu.assistant.base.BaseApplion;
import com.stu.assistant.bean.TableBean;
import com.stu.assistant.bean.TableTimeBean;
import com.stu.assistant.bean.UserInfoBean;
import com.stu.assistant.constants.ConstansUserManager;
import com.stu.assistant.uitls.CalendarUtil;
import com.stu.assistant.uitls.MyToast;
import com.stu.assistant.uitls.SharedConfig;
import com.stu.assistant.uitls.SharedUtil;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

//添加具体的节课的页面
public class AddTableDialogActivity extends BaseActivity {


    private EditText tableNameEdit;
    private EditText addressEdit, markEdit;
    private Spinner spinner, spinnerDay, teacherNameSpinner;
    private TableBean bean;
    private ArrayList<UserInfoBean> teacherList;
    private TextView btn;
    private String item = "1";

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

    @Override
    protected void initView() {
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        item = intent.getStringExtra("item");
        String titleMsg = "";
        bean = (TableBean) intent.getSerializableExtra("bean");
        if (title.equals("1")) {
            titleMsg = "新增课程";
        } else {
            titleMsg = "编辑课程";
        }
        setTitleLay(R.id.title_tv, R.id.back_lay, titleMsg);
        setTextMsg(R.id.OnPushClick, titleMsg);
        tableNameEdit = findViewById(R.id.edit_table_name);
        teacherNameSpinner = findViewById(R.id.edit_teacherName);
        addressEdit = findViewById(R.id.edit_address);
        markEdit = findViewById(R.id.edit_mark);
        spinner = findViewById(R.id.spinner_time);
        spinnerDay = findViewById(R.id.spinner_day);
        btn = findViewById(R.id.OnPushClick);
        spinner.setEnabled(false);
        spinnerDay.setEnabled(false);
    }

    @Override
    protected void initData() {
        spinnerDay.setSelection(bean.getDay());
        spinner.setSelection(bean.getPos() + 1);
        if (!TextUtils.isEmpty(bean.getAddress())) {
            addressEdit.setText(bean.getAddress());
        }
        if (!TextUtils.isEmpty(bean.getTableNameMsg())) {
            tableNameEdit.setText(bean.getTableNameMsg());
        }

        if (!TextUtils.isEmpty(bean.getMark())) {
            markEdit.setText(bean.getMark());
        }
        showProDialog("正在查询老师信息");
        BmobQuery<UserInfoBean> query = new BmobQuery<>();
        query.addWhereEqualTo("userLevel", ConstansUserManager.Level_3);
        query.order("-createdAt")
                .findObjects(new FindListener<UserInfoBean>() {
                    @Override
                    public void done(List<UserInfoBean> list, BmobException e) {
                        dismissProDialog();
                        teacherList = (ArrayList<UserInfoBean>) list;
                        setSpinner(teacherList);
                    }
                });
    }

    private void setSpinner(ArrayList<UserInfoBean> list) {
        UserInfoBean userInfoBean = new UserInfoBean();
        userInfoBean.setUsername("请选择老师");
        list.add(0, userInfoBean);
        String[] mItems = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            mItems[i] = list.get(i).getUsername();
        }
        ArrayAdapter<String> _Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mItems);
        teacherNameSpinner.setAdapter(_Adapter);
        //默认选择带过来的老师信息
        if (!TextUtils.isEmpty(bean.getTeacherId())) {
            for (int i = 0; i < list.size(); i++) {
                String objectId = list.get(i).getObjectId();
                if (!TextUtils.isEmpty(objectId)) {
                    if (objectId.equals(bean.getTeacherId())) {
                        teacherNameSpinner.setSelection(i);
                        break;
                    }
                }
            }
        }
    }

    @Override
    protected void initListener() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tableNameMsg = tableNameEdit.getText().toString();
                String addressMsg = addressEdit.getText().toString();
                String markMsg = markEdit.getText().toString();
                int teacherName = teacherNameSpinner.getSelectedItemPosition();
                int selectedItemPosition = spinner.getSelectedItemPosition();
                int selectedItemPosition1 = spinnerDay.getSelectedItemPosition();
                if (teacherList == null) {
                    MyToast.show("请重新加载老师信息");
                    return;
                }
                if (TextUtils.isEmpty(tableNameMsg)) {
                    MyToast.show(tableNameEdit.getHint().toString());
                    return;
                }

                if (TextUtils.isEmpty(addressMsg)) {
                    MyToast.show(addressEdit.getHint().toString());
                    return;
                }

                if (teacherName == 0) {
                    MyToast.show("请选择老师");
                    return;
                }
                if (selectedItemPosition == 0) {
                    MyToast.show("请选择周数");
                    return;
                }

                if (selectedItemPosition1 == 0) {
                    MyToast.show("请选择节次");
                    return;
                }
                bean.setTeacherId(teacherList.get(teacherName).getObjectId());
                bean.setTeacherName(teacherList.get(teacherName).getUsername());
                bean.setAddress(addressMsg);
                bean.setTableNameMsg(tableNameMsg);
                bean.setStuId(SharedUtil.create(BaseApplion.application).getString(SharedConfig.objectId));
                bean.setMark(markMsg);
//                Log.i("hx","--item-->"+item);
//                Log.i("hx","--bean-->"+new Gson().toJson(bean));
                if (TextUtils.isEmpty(item) || item.equals("1")) {//直接返回一块更新
                    showProDialog("正在保存");
                    bean.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            dismissProDialog();
                            if (e == null) {
                                Intent intent = new Intent();
                                bean.setObjectId(s);
                                intent.putExtra("bean", bean);
                                setResult(12, intent);
                                finish();
                            } else {
                                MyToast.show("保存失败" + e.getMessage());
                            }
                        }
                    });
                } else {//单独更新
                    showProDialog("正在更新");
                    bean.update(bean.getObjectId(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            dismissProDialog();
                            if (e == null) {
                                Intent intent = new Intent();
                                intent.putExtra("bean", bean);
                                setResult(12, intent);
                                finish();
                            } else {
                                MyToast.show("失败" + e.getMessage());
                            }
                        }
                    });
                }
            }
        });
    }

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