package com.wisewoods.eduandroid.ui;

import com.blankj.utilcode.util.ActivityUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wisewoods.base.bean.BaseEvent;
import com.wisewoods.base.bean.MettingInfo;
import com.wisewoods.base.bean.Results;
import com.wisewoods.base.net.JCallback;
import com.wisewoods.base.net.JDCallback;
import com.wisewoods.base.page.BaseListActivity;
import com.wisewoods.base.utils.$;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityMettingBinding;
import com.wisewoods.eduandroid.ui.adapter.MettingAdapter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import retrofit2.Call;
import retrofit2.Response;

public class MettingActivity extends BaseListActivity<ActivityMettingBinding> {

    private MettingAdapter mAdapter;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(BaseEvent.MeetingEvent event) {
        onRefresh();
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void initListData() {
        rv = binding.rvMetting;
        srf = binding.srf;

        showBarBack(binding.bar.tvTitle, "会议", binding.bar.ivBack);

        binding.cardCreate.setOnClickListener(v -> {
            ActivityUtils.startActivity(CreateMettingActivity.class);
        });
        binding.cardHistory.setOnClickListener(v -> {
            ActivityUtils.startActivity(MettingHistoryActivity.class);
        });

        mAdapter = new MettingAdapter();
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tv_join) {
                String joinUrl = mAdapter.getItem(position).joinUrl;
                WebActivity.start(joinUrl, "");

            } else if (view.getId() == R.id.tv_cancel) {
                cancelMetting(position, mAdapter.getItem(position).id + "");
            }
        });
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    protected void getData(long page, long size) {
        App.api.selectMeetingsStartedList(page, size).enqueue(new JCallback<MettingInfo>() {
            @Override
            public void onS(Response<MettingInfo> response, MettingInfo mettingInfo) {
                getDataSuccess(mettingInfo.data.rows, mettingInfo.data.total);
            }

            @Override
            public void onFailure(Call<MettingInfo> call, Throwable t) {
                super.onFailure(call, t);
                getDataFail();
            }
        });
    }

    private void cancelMetting(int position, String id) {
        App.api.cancelMetting(id)
                .enqueue(new JDCallback<Results>() {
                    @Override
                    public void onS(Response<Results> response, Results results) {
                        $.toast("成功取消会议");
                        mAdapter.removeAt(position);
                    }
                });
    }
}
