package xn.hxp.ui.plan.change;

import android.content.Intent;
import android.os.Bundle;

import androidx.viewbinding.ViewBinding;
import androidx.viewpager2.widget.ViewPager2;

import com.rc.core.ui.activity.BaseActivity;

import java.util.List;

import xn.hxp.databinding.ActivityChangeCabinetBinding;
import xn.hxp.ui.plan.FragmentCallBack;
import xn.hxp.ui.plan.room.RoomTool;
import xn.hxp.ui.plan.room.bean.locker.CabinetLayerSelect;
import xn.hxp.ui.plan.room.bean.locker.HxpCabinetVo;

/**
 * 更换位置
 */
public class ChangeCabinetActivity extends BaseActivity {
    private ActivityChangeCabinetBinding binding;
    private ChangeCabinetAdapter changeCabinetAdapter;
    private CabinetLayerSelect cabinetLayerSelect;

    @Override
    protected void onInit() {
        List<HxpCabinetVo> hxpCabinetVoList = RoomTool.getInstance().hxpCabinetDAO().getAll();
        changeCabinetAdapter = new ChangeCabinetAdapter(this, hxpCabinetVoList, fragmentCallBack);
        binding.cabinetVP.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.countTV.setText((position + 1) + "/" + changeCabinetAdapter.getItemCount());
            }
        });
        binding.nextBT.setOnClickListener(view -> binding.cabinetVP.setCurrentItem(binding.cabinetVP.getCurrentItem() + 1, true));
        binding.lastBT.setOnClickListener(view -> binding.cabinetVP.setCurrentItem(binding.cabinetVP.getCurrentItem() - 1, true));
        binding.cabinetVP.setAdapter(changeCabinetAdapter);

        binding.cancelBT.setOnClickListener(v -> finish());

        binding.confirmBT.setOnClickListener(v -> {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("Change", cabinetLayerSelect);
            intent.putExtra("data", bundle);
            setResult(RESULT_OK, intent);
            finish();
        });
    }

    FragmentCallBack fragmentCallBack = new FragmentCallBack() {
        @Override
        public void onSelected(CabinetLayerSelect lastSelectInfo) {
            cabinetLayerSelect = lastSelectInfo;
        }
    };

    @Override
    protected void cdFinish() {
        finish();
    }

    @Override
    protected void cdTime(int cd) {

    }

    @Override
    protected ViewBinding setViewBinding() {
        return binding = ActivityChangeCabinetBinding.inflate(getLayoutInflater());
    }
}