package com.tenlionsoft.oamodule.activity.routine;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;
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.beans.AddFileBean;
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.UserLgUtils;
import com.tenlionsoft.oamodule.R;
import com.tenlionsoft.oamodule.R2;
import com.tenlionsoft.oamodule.beans.RoutineAcceptListBean;
import com.tenlionsoft.oamodule.beans.RoutineBean;
import com.tenlionsoft.oamodule.net.OAApi;

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

import androidx.appcompat.widget.SwitchCompat;
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月18日11:32:54
 * 邮箱: itgaojian@163.com
 * 描述: 日常工作-详情页面
 */
@Route(path = PathConfig.PATH_MODULE_OA_ACTIVITY_ROUTINE_WORK_DETAIL)
public class RoutineWorkDetailActivity extends BaseActivity {


    @BindView(R2.id.tv_name)
    TextView mTvName;
    @BindView(R2.id.tv_start_time)
    TextView mTvStartTime;
    @BindView(R2.id.tv_end_time)
    TextView mTvEndTime;
    @BindView(R2.id.tv_tl)
    TextView mTvTl;
    @BindView(R2.id.tv_undertake)
    TextView mTvUndertake;
    @BindView(R2.id.tv_assisted)
    TextView mTvAssisted;
    @BindView(R2.id.sw_schedule)
    SwitchCompat mSwSchedule;
    @BindView(R2.id.tv_request)
    TextView mTvRequest;
    @BindView(R2.id.tv_remark)
    TextView mTvRemark;
    @BindView(R2.id.rlv_files)
    RecyclerView mRlvFiles;
    @BindView(R2.id.tv_restore)
    TextView mTvRestore;
    @BindView(R2.id.rlv_restore_files)
    RecyclerView mRlvRestoreFiles;
    @BindView(R2.id.ll_restore)
    LinearLayout mLlRestore;
    @BindView(R2.id.tv_file_hint)
    TextView mTvFileHint;
    @BindView(R2.id.tv_restore_file_hint)
    TextView mTvRestoreFileHint;
    @BindView(R2.id.tv_restore_time)
    TextView mTvRestoreTime;
    @BindView(R2.id.tv_restore_user_type)
    TextView mTvRestoreUserType;

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

    @Override
    public void initData() {
        ButterKnife.bind(this);
        mTvBaseTitle.setText("工作详情");
        String id = getIntent().getStringExtra("id");
        String userId = getIntent().getStringExtra("userId");
        if (TextUtils.isEmpty(userId)) {
            //我发起的日常工作
            getRoutineById(id);
        } else {
            //接收的日常工作
            getRoutineByAcceptId(userId);
        }
    }

    private void getRoutineByAcceptId(String userId) {
        RetrofitManager.getInstance()
            .create(OAApi.class)
            .getRoutineAcceptDetail(userId, UserLgUtils.getToken())
            .compose(RxTransformer.getTransformer())
            .subscribe(new Observer<RoutineAcceptListBean.RowsBean>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(RoutineAcceptListBean.@NonNull RowsBean rowsBean) {
                    setCompleteDataToView(rowsBean);
                }

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

                @Override
                public void onComplete() {

                }
            });
    }

    /**
     * 回显数据
     */
    private void setCompleteDataToView(RoutineAcceptListBean.RowsBean b) {
        refreshView(STATE_LOAD_SUCCESS);
        mTvName.setText(b.getRoutineWorkDTO().getWorkName());
        mTvStartTime.setText(b.getRoutineWorkDTO().getWorkCompleteStartTime());
        mTvEndTime.setText(b.getRoutineWorkDTO().getWorkCompleteEndTime());
        mTvTl.setText(b.getRoutineWorkDTO().getWorkCompleteDay() + "");
        mTvUndertake.setText(b.getRoutineWorkDTO().getWorkUndertakerUserName());
        mTvAssisted.setText(b.getRoutineWorkDTO().getWorkSponsorUserNames());
        mSwSchedule.setChecked(b.getRoutineWorkDTO().getIsSchedule().equals("1"));
        mTvRequest.setText(Html.fromHtml(b.getRoutineWorkDTO().getWorkResultReq()));
        mTvRemark.setText(Html.fromHtml(b.getRoutineWorkDTO().getWorkRemarks()));
        if (!TextUtils.isEmpty(b.getRoutineWorkDTO().getWorkFiles())) {
            List<AddFileBean> files = new ArrayList<>();
            mTvFileHint.setVisibility(View.GONE);
            mRlvFiles.setVisibility(View.VISIBLE);
            String[] split = b.getRoutineWorkDTO().getWorkFiles().split(",");
            for (int i = 0; i < split.length; i++) {
                AddFileBean bean = new AddFileBean();
                bean.setId(split[i]);
                bean.setPath(BaseUrlApi.BASE_IMG_URL + split[i]);
                bean.setFileType(1);
                files.add(bean);
            }
            ShowFileAdapter adapter = new ShowFileAdapter(mActivity, files);
            mRlvFiles.setLayoutManager(new GridLayoutManager(mActivity, 4));
            mRlvFiles.setAdapter(adapter);
        } else {
            mTvFileHint.setVisibility(View.VISIBLE);
            mRlvFiles.setVisibility(View.GONE);
        }
        mTvRestore.setText(Html.fromHtml(b.getContent()));
        mTvRestoreTime.setText(b.getGmtCreate());
        mTvRestoreUserType.setText("undertaker".equals(b.getUserType()) ? "承办人员" : "协办人员");
        if (!TextUtils.isEmpty(b.getFiles())) {
            mRlvRestoreFiles.setVisibility(View.VISIBLE);
            mTvRestoreFileHint.setVisibility(View.GONE);
            List<AddFileBean> files = new ArrayList<>();
            String[] split = b.getRoutineWorkDTO().getWorkFiles().split(",");
            for (int i = 0; i < split.length; i++) {
                AddFileBean bean = new AddFileBean();
                bean.setId(split[i]);
                bean.setPath(BaseUrlApi.BASE_IMG_URL + split[i]);
                bean.setFileType(1);
                files.add(bean);
            }
            ShowFileAdapter adapter = new ShowFileAdapter(mActivity, files);
            mRlvRestoreFiles.setLayoutManager(new GridLayoutManager(mActivity, 4));
            mRlvRestoreFiles.setAdapter(adapter);
        } else {
            mRlvRestoreFiles.setVisibility(View.GONE);
            mTvRestoreFileHint.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 获取日常工作详情
     */
    private void getRoutineById(String id) {
        RetrofitManager.getInstance()
            .create(OAApi.class)
            .getRoutineDetail(id, UserLgUtils.getToken())
            .compose(RxTransformer.getTransformer())
            .subscribe(new Observer<RoutineBean>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull RoutineBean routineBean) {
                    setDataToView(routineBean);
                }

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

                @Override
                public void onComplete() {

                }
            });
    }

    private void setDataToView(RoutineBean b) {
        refreshView(STATE_LOAD_SUCCESS);
        mLlRestore.setVisibility(View.GONE);
        mTvName.setText(b.getWorkName());
        mTvStartTime.setText(b.getWorkCompleteStartTime());
        mTvEndTime.setText(b.getWorkCompleteEndTime());
        mTvTl.setText(b.getWorkCompleteDay() + "");
        mTvUndertake.setText(b.getWorkUndertakerUserName());
        mTvAssisted.setText(b.getWorkSponsorUserNames());
        mSwSchedule.setChecked(b.getIsSchedule().equals("1"));
        mTvRequest.setText(Html.fromHtml(b.getWorkResultReq()));
        mTvRemark.setText(Html.fromHtml(b.getWorkRemarks()));
        if (!TextUtils.isEmpty(b.getWorkFiles())) {
            mTvFileHint.setVisibility(View.GONE);
            mRlvFiles.setVisibility(View.VISIBLE);
            List<AddFileBean> files = new ArrayList<>();
            String[] split = b.getWorkFiles().split(",");
            for (int i = 0; i < split.length; i++) {
                AddFileBean bean = new AddFileBean();
                bean.setId(split[i]);
                bean.setPath(BaseUrlApi.BASE_IMG_URL + split[i]);
                bean.setFileType(1);
                files.add(bean);
            }
            ShowFileAdapter adapter = new ShowFileAdapter(mActivity, files);
            mRlvFiles.setLayoutManager(new GridLayoutManager(mActivity, 4));
            mRlvFiles.setAdapter(adapter);
        } else {
            mTvFileHint.setVisibility(View.VISIBLE);
            mRlvFiles.setVisibility(View.GONE);
        }
    }

    @Override
    protected void doSearchByTitle() {

    }

    @Override
    protected void clearSearch() {

    }
}