package com.tenlionsoft.oamodule.activity.workreports;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.constant.PathConfig;
import com.tenlionsoft.baselib.core.beans.AddFileBean;
import com.tenlionsoft.baselib.core.beans.BaseSuccessBean;
import com.tenlionsoft.baselib.core.retrofit_net.BaseUrlApi;
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.core.widget.base.ShowFileAdapter;
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.WorkReportDetailBean;
import com.tenlionsoft.oamodule.net.OAApi;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
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月19日15:21:37
 * 邮箱: itgaojian@163.com
 * 描述: 工作报告详情
 */
@Route(path = PathConfig.PATH_MODULE_OA_ACTIVITY_WORK_REPORT_DETAIL)
public class WorkReportDetailActivity extends BaseActivity {


    @BindView(R2.id.tv_name)
    TextView mTvName;
    @BindView(R2.id.tv_type)
    TextView mTvType;
    @BindView(R2.id.et_out)
    TextView mEtOut;
    @BindView(R2.id.tv_content)
    TextView mTvContent;
    @BindView(R2.id.rlv_files)
    RecyclerView mRlvFiles;
    @BindView(R2.id.btn_del)
    Button mBtnDel;
    @BindView(R2.id.btn_edit)
    Button mBtnEdit;
    @BindView(R2.id.tv_file_hint)
    TextView mTvFileHint;
    private String mId;

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

    @Override
    public void initData() {
        ButterKnife.bind(this);
        mTvBaseTitle.setText("工作报告");
        mId = getIntent().getStringExtra("id");
        if (TextUtils.isEmpty(mId)) {
            ToastUtils.show("数据有误");
            finish();
        } else {
            getDetailById();
        }
        mBtnDel.setOnClickListener(v -> showDelConfirm());
        mBtnEdit.setOnClickListener(v -> ARouter.getInstance()
            .build(PathConfig.PATH_MODULE_OA_ACTIVITY_WORK_REPORT_EDIT)
            .withString("id", mId)
            .navigation(mActivity, 12));
    }

    private void showDelConfirm() {
        new AlertDialog.Builder(mActivity)
            .setTitle("提示")
            .setMessage("确定要删除该报告吗?")
            .setPositiveButton("确定", (dialog, which) -> {
                dialog.dismiss();
                doDel();
            })
            .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
            .create()
            .show();
    }

    private void doDel() {
        ProgressDialog dialog = UIUtil.initDialog(mActivity, "删除中...");
        dialog.show();
        RetrofitManager.getInstance()
            .create(OAApi.class)
            .doDelWorkReport(mId, 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 void getDetailById() {
        RetrofitManager.getInstance()
            .create(OAApi.class)
            .getWorkReportDetail(mId, UserLgUtils.getToken())
            .compose(RxTransformer.getTransformer())
            .subscribe(new Observer<WorkReportDetailBean>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull WorkReportDetailBean workReportDetailBean) {
                    setDataToView(workReportDetailBean);
                }

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

                @Override
                public void onComplete() {

                }
            });
    }

    private void setDataToView(WorkReportDetailBean bean) {
        refreshView(STATE_LOAD_SUCCESS);
        mTvName.setText(bean.getReportTitle());
        switch (bean.getReportType()) {
            case "1":
                mTvType.setText("工作日报");
                break;
            case "2":
                mTvType.setText("工作周报");
                break;
            case "3":
                mTvType.setText("工作月报");
                break;
        }
        mEtOut.setText(Html.fromHtml(bean.getReportEgress()));
        mTvContent.setText(Html.fromHtml(bean.getReportContent()));
        //文件
        if (!TextUtils.isEmpty(bean.getReportFiles())) {
            mRlvFiles.setVisibility(View.VISIBLE);
            mTvFileHint.setVisibility(View.GONE);
            List<AddFileBean> files = new ArrayList<>();
            String[] split = bean.getReportFiles().split(",");
            for (int i = 0; i < split.length; i++) {
                AddFileBean fileBean = new AddFileBean();
                fileBean.setId(split[i]);
                fileBean.setPath(BaseUrlApi.BASE_IMG_URL + split[i]);
                fileBean.setFileType(1);
                files.add(fileBean);
            }
            ShowFileAdapter adapter = new ShowFileAdapter(mActivity, files);
            mRlvFiles.setLayoutManager(new GridLayoutManager(mActivity, 4));
            mRlvFiles.setAdapter(adapter);
        } else {
            mRlvFiles.setVisibility(View.GONE);
            mTvFileHint.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            if (requestCode == 12) {
                refreshView(STATE_LOAD_LOADING);
                getDetailById();
            }
        }
    }

    @Override
    protected void doSearchByTitle() {

    }

    @Override
    protected void clearSearch() {

    }
}