package com.tenlionsoft.oamodule.activity.supervise;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.tenlionsoft.baselib.constant.PathConfig;
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.UserLgUtils;
import com.tenlionsoft.oamodule.R;
import com.tenlionsoft.oamodule.R2;
import com.tenlionsoft.oamodule.beans.SuperviseBean;
import com.tenlionsoft.oamodule.beans.SuperviseRepectBean;
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;

/**
 * 作者: Adam
 * 日期: 2022年04月18日18:07:56
 * 邮箱: itgaojian@163.com
 * 描述: 督办详情
 */
@Route(path = PathConfig.PATH_MODULE_OA_ACTIVITY_SUPERVISE_DETAIL)
public class SuperviseDetailActivity 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.tv_repect_content)
    TextView mTvRepectContent;
    @BindView(R2.id.ll_repect)
    LinearLayout mLlRepect;
    private String mId;
    private String mUserId;

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

    @Override
    public void initData() {
        ButterKnife.bind(this);
        mTvBaseTitle.setText("督办详情");
        mId = getIntent().getStringExtra("id");
        mUserId = getIntent().getStringExtra("userId");
        if (!TextUtils.isEmpty(mUserId)) {
            getDetailByUserId();
        } else {
            getDetailById();
        }
    }

    private void getDetailById() {
        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 getDetailByUserId() {
        RetrofitManager.getInstance()
            .create(OAApi.class)
            .getSuperviseRepectDetail(mUserId, UserLgUtils.getToken())
            .compose(RxTransformer.getTransformer())
            .subscribe(new Observer<SuperviseRepectBean>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull SuperviseRepectBean superviseBean) {
                    setRepectDataToView(superviseBean);
                }

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

                @Override
                public void onComplete() {

                }
            });
    }

    private void setRepectDataToView(SuperviseRepectBean bean) {
        refreshView(STATE_LOAD_SUCCESS);
        mTvName.setText(bean.getOverseeDTO().getOverseeTitle());
        mTvType.setText(bean.getOverseeDTO().getOverseeTypeName());
        mTvEndTime.setText(bean.getOverseeDTO().getOverseeCompleteTime());
        mTvRelevance.setText(bean.getOverseeDTO().getOverseeItemName());
        mTvUsers.setText(bean.getOverseeDTO().getOverseeUserNames());
        mTvRequest.setText(bean.getOverseeDTO().getOverseeContent());
        mCbNoticeMsg.setChecked(bean.getOverseeDTO().getOverseeNoticeType().contains("1"));
        mCbNoticeEmail.setChecked(bean.getOverseeDTO().getOverseeNoticeType().contains("2"));
        mLlRepect.setVisibility(View.VISIBLE);
        mTvRepectContent.setText(bean.getContent());
    }

    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"));
        mLlRepect.setVisibility(View.GONE);
    }


    @Override
    protected void doSearchByTitle() {

    }

    @Override
    protected void clearSearch() {

    }
}