package com.cooleshow.teacher.widgets;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.cooleshow.base.utils.KeyboardUtils;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.databinding.DialogSetCourseCommentLayoutBinding;

import androidx.annotation.NonNull;

/**
 * Author by pq, Date on 2022/4/27.
 */
public class CourseSetCommentDialog extends Dialog implements View.OnClickListener {
    public static final int TYPE_SET_TEACHER_COMMENT = 0;//老师评价
    public static final int TYPE_SET_HOMEWORK = 1;//布置作业
    public static final int TYPE_SET_HOMEWORK_COMMENT = 2;//作业点评

    private OnSubmitClickListener mListener;
    private int showType = TYPE_SET_TEACHER_COMMENT;

    private DialogSetCourseCommentLayoutBinding mViewBinding;

    public CourseSetCommentDialog(@NonNull Context context) {
        super(context, com.cooleshow.base.R.style.DialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewBinding = DialogSetCourseCommentLayoutBinding.inflate(getLayoutInflater());
        setContentView(mViewBinding.getRoot());
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        mViewBinding.ivClose.setOnClickListener(this);
        mViewBinding.tvSubmit.setOnClickListener(this);
        mViewBinding.flContent.setOnClickListener(this);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (mViewBinding != null) {
                    if (mViewBinding.etContent != null) {
                        mViewBinding.etContent.clearFocus();
                        KeyboardUtils.hideSoftInput(mViewBinding.etContent);
                    }
                }
            }
        });
    }


    public void unbind() {
        if (mViewBinding != null) {
            mViewBinding = null;
        }
    }

    public void showMethod(int method) {
        showType = method;
        mViewBinding.etContent.setText("");
        if (method == TYPE_SET_TEACHER_COMMENT) {
            //老师提交评价
            mViewBinding.tvTitle.setText("评价学员");
            mViewBinding.etContent.setHint("请输入您对本次课程学员表现的评价");
        }
        if (method == TYPE_SET_HOMEWORK) {
            //老师布置作业
            mViewBinding.tvTitle.setText("布置作业");
            mViewBinding.etContent.setHint("请输入您输入本次课程需要布置的作业内容");
        }
        if (method == TYPE_SET_HOMEWORK_COMMENT) {
            //老师提交作业评价
            mViewBinding.tvTitle.setText("作业点评");
            mViewBinding.etContent.setHint("请输入您对本次课程学员的作业点评");
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_close) {
            dismiss();
            return;
        }

        if (v.getId() == R.id.fl_content) {
            KeyboardUtils.showSoftInput(mViewBinding.etContent);
            return;
        }

        if (v.getId() == R.id.tv_submit) {
            //提交
            String content = mViewBinding.etContent.getText().toString();
            if (TextUtils.isEmpty(content)) {
                ToastUtils.showShort("请输入需要提交的内容");
                return;
            }
            if (mListener != null) {
                mListener.onSubmit(content, showType);
            }
            dismiss();
            return;
        }
    }

    public void setOnSubmitClickListener(OnSubmitClickListener submitClickListener) {
        this.mListener = submitClickListener;
    }

    public interface OnSubmitClickListener {
        void onSubmit(String content, int type);
    }
}
