package com.stu.assistant.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
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.TaskBean;
import com.stu.assistant.dialog.DiaogUtil;
import com.stu.assistant.uitls.MyToast;
import com.stu.assistant.uitls.SharedConfig;
import com.stu.assistant.uitls.SharedUtil;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
//添加作业页面
public class AddTaskActivity extends BaseActivity {
    private EditText taskNameEdit;
    private EditText msgEdit;
    private TaskBean bean;
    private TextView btn, timeTv;

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

    @Override
    protected void initView() {
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String titleMsg = "";
        bean = (TaskBean) intent.getSerializableExtra("bean");
        if (bean == null) {
            titleMsg = "新增作业";
        } else {
            titleMsg = "编辑作业";
        }
        setTitleLay(R.id.title_tv, R.id.back_lay, titleMsg);
        setTextMsg(R.id.OnPushClick, titleMsg);
        taskNameEdit = findViewById(R.id.edit_task_name);
        msgEdit = findViewById(R.id.edit_msg);
        timeTv = findViewById(R.id.tv_time);
        btn = findViewById(R.id.OnPushClick);
    }

    @Override
    protected void initData() {
        if (bean != null) {
            if (!TextUtils.isEmpty(bean.getTaskMsg())) {
                msgEdit.setText(bean.getTaskMsg());
            }
            if (!TextUtils.isEmpty(bean.getTableNameMsg())) {
                taskNameEdit.setText(bean.getTableNameMsg());
            }
            if (!TextUtils.isEmpty(bean.getTime())) {
                timeTv.setText(bean.getTime());
            }
        }
    }

    @Override
    protected void initListener() {
        timeTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DiaogUtil.showSelectTimeDialog(AddTaskActivity.this, new DiaogUtil.OnSelectLisenter() {
                    @Override
                    public void OnOkBtn(String time) {
                        timeTv.setText(time);
                    }
                });
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tableNameMsg = taskNameEdit.getText().toString();
                String taskMsg = msgEdit.getText().toString();
                String timeMsg = timeTv.getText().toString();
                if (TextUtils.isEmpty(tableNameMsg)) {
                    MyToast.show(taskNameEdit.getHint().toString());
                    return;
                }

                if (TextUtils.isEmpty(timeMsg)) {
                    MyToast.show(timeTv.getHint().toString());
                    return;
                }

                if (TextUtils.isEmpty(taskMsg)) {
                    MyToast.show(msgEdit.getHint().toString());
                    return;
                }

                if (bean == null) {//直接返回一块更新
                    TaskBean bean = new TaskBean();
                    bean.setTaskMsg(taskMsg);
                    bean.setTime(timeMsg);
                    bean.setTableNameMsg(tableNameMsg);
                    bean.setStuId(SharedUtil.create(BaseApplion.application).getString(SharedConfig.objectId));
                    Log.i("hx", "--bean-->" + new Gson().toJson(bean));
                    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.setTaskMsg(taskMsg);
                    bean.setTime(timeMsg);
                    bean.setTableNameMsg(tableNameMsg);
                    bean.setStuId(SharedUtil.create(BaseApplion.application).getString(SharedConfig.objectId));
                    Log.i("hx", "--bean-->" + new Gson().toJson(bean));
                    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();
    }
}