package com.tenlionsoft.oamodule.activity.supervise;

import android.app.ProgressDialog;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.gson.Gson;
import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.constant.PathConfig;
import com.tenlionsoft.baselib.core.beans.BaseSuccessBean;
import com.tenlionsoft.baselib.core.retrofit_net.RetrofitManager;
import com.tenlionsoft.baselib.core.retrofit_net.conver.RxTransformer;
import com.tenlionsoft.baselib.core.widget.base.BaseActivity;
import com.tenlionsoft.baselib.utils.ExceptionHandler;
import com.tenlionsoft.baselib.utils.UIUtil;
import com.tenlionsoft.baselib.utils.UserLgUtils;
import com.tenlionsoft.oamodule.R;
import com.tenlionsoft.oamodule.R2;
import com.tenlionsoft.oamodule.beans.SaveRepectSuperviseBean;
import com.tenlionsoft.oamodule.beans.SuperviseBean;
import com.tenlionsoft.oamodule.net.OAApi;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 作者: Adam
 * 日期: 2022年04月18日18:25:41
 * 邮箱: itgaojian@163.com
 * 描述: 督办回复
 */
@Route(path = PathConfig.PATH_MODULE_OA_ACTIVITY_SUPERVISE_TRANSACT)
public class SuperviseTransactActivity extends BaseActivity {


    @BindView(R2.id.tv_name)
    TextView mTvName;
    @BindView(R2.id.tv_type)
    TextView mTvType;
    @BindView(R2.id.tv_end_time)
    TextView mTvEndTime;
    @BindView(R2.id.tv_relevance)
    TextView mTvRelevance;
    @BindView(R2.id.tv_users)
    TextView mTvUsers;
    @BindView(R2.id.tv_request)
    TextView mTvRequest;
    @BindView(R2.id.cb_notice_msg)
    CheckBox mCbNoticeMsg;
    @BindView(R2.id.cb_notice_email)
    CheckBox mCbNoticeEmail;
    @BindView(R2.id.et_repect_content)
    EditText mEtRepectContent;
    @BindView(R2.id.btn_confirm)
    Button mBtnConfirm;
    private String mId;
    private String mUserId;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_supervise_transact;
    }

    @Override
    public void initData() {
        ButterKnife.bind(this);
        mTvBaseTitle.setText("督办回复");
        mId = getIntent().getStringExtra("id");
        mUserId = getIntent().getStringExtra("userId");
        if (TextUtils.isEmpty(mId)) {
            ToastUtils.show("数据有误");
            finish();
        } else {
            getDetailData();
        }
        initViews();
    }

    private void initViews() {
        mBtnConfirm.setOnClickListener(v -> doConfirm());
    }

    private void doConfirm() {
        if (checkParams()) {
            ProgressDialog dialog = UIUtil.initDialog(mActivity, "提交中...");
            dialog.show();
            RequestBody body = buildParams();
            RetrofitManager.getInstance()
                .create(OAApi.class)
                .doSaveRepectSupervise(mUserId, body, UserLgUtils.getToken())
                .compose(RxTransformer.getTransformer())
                .subscribe(new Observer<BaseSuccessBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull BaseSuccessBean baseSuccessBean) {
                        if (dialog.isShowing()) dialog.dismiss();
                        ToastUtils.show("回复成功");
                        setResult(RESULT_OK);
                        finish();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if (dialog.isShowing()) dialog.dismiss();
                        ExceptionHandler.handleException(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

        }
    }

    private boolean checkParams() {
        String content = mEtRepectContent.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.show("请输入回复内容");
            return false;
        }
        return true;
    }

    private RequestBody buildParams() {
        SaveRepectSuperviseBean bean = new SaveRepectSuperviseBean();
        String content = mEtRepectContent.getText().toString().trim();
        bean.setContent(content);
        bean.setOverseeId(mId);
        bean.setUserId(UserLgUtils.getUserId());
        bean.setUserName(UserLgUtils.getName());
        return RequestBody.create(new Gson().toJson(bean), MediaType.parse("application/json; charset=utf-8"));
    }

    private void getDetailData() {
        RetrofitManager.getInstance()
            .create(OAApi.class)
            .getSuperviseDetail(mId, UserLgUtils.getToken())
            .compose(RxTransformer.getTransformer())
            .subscribe(new Observer<SuperviseBean>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull SuperviseBean superviseBean) {
                    setDataToView(superviseBean);
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    refreshView(STATE_LOAD_ERROR);
                    ExceptionHandler.handleException(e);
                }

                @Override
                public void onComplete() {

                }
            });
    }

    private void setDataToView(SuperviseBean bean) {
        refreshView(STATE_LOAD_SUCCESS);
        mTvName.setText(bean.getOverseeTitle());
        mTvType.setText(bean.getOverseeTypeName());
        mTvEndTime.setText(bean.getOverseeCompleteTime());
        mTvRelevance.setText(bean.getOverseeItemName());
        mTvUsers.setText(bean.getOverseeUserNames());
        mTvRequest.setText(bean.getOverseeContent());
        mCbNoticeMsg.setChecked(bean.getOverseeNoticeType().contains("1"));
        mCbNoticeEmail.setChecked(bean.getOverseeNoticeType().contains("2"));

    }

    @Override
    protected void doSearchByTitle() {

    }

    @Override
    protected void clearSearch() {

    }
}