package com.tenlionsoft.oamodule.activity.meeting;

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

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hjq.toast.ToastUtils;
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.adapter.BasePersonShowAdapter;
import com.tenlionsoft.oamodule.beans.MeetingRoomRecordBean;
import com.tenlionsoft.oamodule.beans.PersonShowBean;
import com.tenlionsoft.oamodule.net.OAApi;

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

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

@Route(path = PathConfig.PATH_MODULE_OA_ACTIVITY_MEETING_DETAIL)
public class MeetingDetailActivity extends BaseActivity {


    @BindView(R2.id.tv_name)
    TextView mTvName;
    @BindView(R2.id.tv_type)
    TextView mTvType;
    @BindView(R2.id.tv_emcee)
    TextView mTvEmcee;
    @BindView(R2.id.tv_start_time)
    TextView mTvStartTime;
    @BindView(R2.id.tv_end_time)
    TextView mTvEndTime;
    @BindView(R2.id.tv_room)
    TextView mTvRoom;
    @BindView(R2.id.cb_notice_msg)
    CheckBox mCbNoticeMsg;
    @BindView(R2.id.cb_notice_email)
    CheckBox mCbNoticeEmail;
    @BindView(R2.id.rlv_join)
    RecyclerView mRlvJoin;
    @BindView(R2.id.tv_content)
    TextView mTvContent;
    @BindView(R2.id.rlv_files)
    RecyclerView mRlvFiles;
    @BindView(R2.id.tv_file_hint)
    TextView mTvFileHint;
    private Unbinder mBind;
    private String mId;
    private List<PersonShowBean> mJoinPerson;
    private BasePersonShowAdapter mPersonShowAdapter;

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

    @Override
    public void initData() {
        mBind = ButterKnife.bind(this);
        mTvBaseTitle.setText("会议详情");
        mId = getIntent().getStringExtra("id");
        if (TextUtils.isEmpty(mId)) {
            ToastUtils.show("数据有误");
            finish();
        } else {
            getMeetingDetail();
        }
    }

    /**
     * 获取会议详情
     */
    private void getMeetingDetail() {
        RetrofitManager.getInstance()
            .create(OAApi.class)
            .getMeetingDetail(mId, UserLgUtils.getToken())
            .compose(RxTransformer.getTransformer())
            .subscribe(new Observer<MeetingRoomRecordBean>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull MeetingRoomRecordBean meetingRoomRecordBean) {
                    setDataToView(meetingRoomRecordBean);
                }

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

                @Override
                public void onComplete() {

                }
            });
    }

    /**
     * 回显数据
     */
    private void setDataToView(MeetingRoomRecordBean b) {
        refreshView(STATE_LOAD_SUCCESS);
        mTvName.setText(b.getMeetingTitle());
        mTvType.setText(b.getMeetingTypeName());
        mTvEmcee.setText(b.getMeetingHostUserName());
        mTvStartTime.setText(b.getMeetingStartTime());
        mTvEndTime.setText(b.getMeetingEndTime());
        mTvRoom.setText(b.getMeetingRoomName());
        if (!TextUtils.isEmpty(b.getMeetingNoticeType())) {
            if (b.getMeetingNoticeType().contains("1")) mCbNoticeMsg.setChecked(true);
            if (b.getMeetingNoticeType().contains("2")) mCbNoticeEmail.setChecked(true);
        }
        if (!TextUtils.isEmpty(b.getMeetingUserIds())) {
            mJoinPerson = new ArrayList<>();
            String[] ids = b.getMeetingUserIds().split("_");
            String[] names = b.getMeetingUserNames().split(",");
            for (int i = 0; i < ids.length; i++) {
                PersonShowBean bean = new PersonShowBean();
                bean.setId(ids[i]);
                bean.setName(names[i]);
                mJoinPerson.add(bean);
            }
            mPersonShowAdapter = new BasePersonShowAdapter(mActivity, mJoinPerson, 2);
            mRlvJoin.setLayoutManager(new GridLayoutManager(mActivity, 4));
            mRlvJoin.setAdapter(mPersonShowAdapter);
        }
        //参会人员
        mTvContent.setText(b.getMeetingContent());
        //文件
        if (!TextUtils.isEmpty(b.getMeetingFile())) {
            mRlvFiles.setVisibility(View.VISIBLE);
            mTvFileHint.setVisibility(View.GONE);
            List<AddFileBean> files = new ArrayList<>();
            String[] split = b.getMeetingFile().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 {
            mRlvFiles.setVisibility(View.GONE);
            mTvFileHint.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void doSearchByTitle() {

    }

    @Override
    protected void clearSearch() {

    }
}