package com.wisewoods.eduandroid.ui;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.luck.picture.lib.tools.ScreenUtils;
import com.wisewoods.base.base.BaseActivity;
import com.wisewoods.base.bean.HotelList;
import com.wisewoods.base.bean.RoomState;
import com.wisewoods.base.net.JDCallback;
import com.wisewoods.base.utils.$;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityHostelBinding;
import com.wisewoods.eduandroid.ui.adapter.HostelAdapter;

import java.util.List;

import retrofit2.Response;

public class HostelActivity extends BaseActivity<ActivityHostelBinding> {

    private HostelAdapter mAdapter;
    private List<HotelList.DataBean> data;
    private String hotelId;

    @Override
    protected void initData(Bundle savedInstanceState) {
        showBarBack(binding.bar.tvTitle, "宿舍占用", binding.bar.ivBack);

        binding.ts.setTimeListener((timeStrStart, timeStrEnd) -> {
            mAdapter.setNewInstance(null);
            getData();
        });

        getHotelData(() -> getData());

        binding.cardHostel.setOnClickListener(v -> {
            showHostel();
        });
        binding.rvHostel.setLayoutManager(new GridLayoutManager(this, 4));
        binding.rvHostel.addItemDecoration(new GridSpacingItemDecoration(4,
                ScreenUtils.dip2px(this, 7), false));
        mAdapter = new HostelAdapter();
        binding.rvHostel.setAdapter(mAdapter);

    }

    private void getHotelData(Runnable runnable) {
        App.api.getHotel("1")
                .enqueue(new JDCallback<HotelList>() {
                    @Override
                    public void onS(Response<HotelList> response, HotelList hotelList) {
                        if (hotelList.data.size() > 0) {
                            data = hotelList.data;
                            binding.cardHostel.setVisibility(View.VISIBLE);
                            binding.tvHostel.setText(hotelList.data.get(0).residenceName);
                            hotelId = hotelList.data.get(0).id + "";
                            if (runnable!=null){
                                runnable.run();
                            }
                        }
                    }
                });
    }

    private void showHostel() {
        if (data == null){
            $.toast("请等待数据加载或者稍后重试");
            getHotelData(null);
        }else {
            OptionsPickerView pvOptions = new OptionsPickerBuilder(activity,
                    (options1, option2, options3, v1) -> {
                        String tx = data.get(options1).residenceName;
                        binding.tvHostel.setText(tx);
                        hotelId = data.get(options1).id + "";
                        mAdapter.setNewInstance(null);
                        getData();
                    }).build();
            pvOptions.setPicker(data);
            pvOptions.show();
        }

    }

    private void getData() {
        App.api.queryEmptyRoom(hotelId, binding.ts.getTimeStrStart(), binding.ts.getTimeStrStart())
                .enqueue(new JDCallback<RoomState>() {
                    @Override
                    public void onS(Response<RoomState> response, RoomState roomState) {
                        mAdapter.setNewInstance(roomState.data);
                    }
                });
    }
}
