package com.rdiot.yx485.device.view;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import com.rdiot.yx485.BaseActivity;
import com.rdiot.yx485.adapter.ContentAdapter;
import com.rdiot.yx485.adapter.DeviceTypeContentAdapter;
import com.rdiot.yx485.bean.DeviceTreeBean;
import com.rdiot.yx485.databinding.ActivityDeviceListBinding;
import com.rdiot.yx485.device.model.BindViewModel;

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

public class DeviceListActivity extends BaseActivity implements ContentAdapter.OnItemClickListener {
    private final List<DeviceTreeBean.DataBean> mDeviceTypeList = new ArrayList<>();
    private final ContentAdapter myAdapter = new ContentAdapter(mDeviceTypeList, this);
    private final DeviceTypeContentAdapter mContentAdapter = new DeviceTypeContentAdapter(mDeviceTypeList, this);
    private RecyclerView.SmoothScroller smoothScroller;
    private LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    private BindViewModel mViewModel = new BindViewModel();
    private ActivityDeviceListBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityDeviceListBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        mBinding.layoutTitle.btnTitleLeft.setOnClickListener(v -> finish());
        mBinding.layoutTitle.tvTitle.setText("设备列表");

        mBinding.leftRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBinding.leftRecyclerView.setAdapter(myAdapter);

        // Initialize ViewPager
        mBinding.rightRecyclerView.setLayoutManager(layoutManager);
        mBinding.rightRecyclerView.setAdapter(mContentAdapter);
        // 创建 SmoothScroller
        smoothScroller = new LinearSmoothScroller(this) {
            @Override
            protected int getVerticalSnapPreference() {
                return LinearSmoothScroller.SNAP_TO_START; // 对齐到顶部
            }
        };

        initData();
    }

    /**
     * 初始化数据
     */
    @SuppressLint("NotifyDataSetChanged")
    private void initData() {
        mViewModel.getErrorMessage().observe(this, this::showMessage);
        mViewModel.getDeviceTypeList().observe(this, dataBeans -> {
            mDeviceTypeList.clear();
            if (dataBeans == null || dataBeans.isEmpty()) {
                return;
            }

            mDeviceTypeList.addAll(dataBeans);
            myAdapter.notifyDataSetChanged();
            mContentAdapter.notifyDataSetChanged();
        });
        mViewModel.loadDeviceTypeList();
    }

    @Override
    public void onItemClick(int position) {
        myAdapter.setSelected(position);
        smoothScroller.setTargetPosition(position);
        // 开始平滑滚动
        layoutManager.startSmoothScroll(smoothScroller);
    }
}