package com.rdiot.yx485.room;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.rdiot.yx485.BaseActivity;
import com.rdiot.yx485.adapter.RoomDeteleDeviceAdapter;
import com.rdiot.yx485.adapter.RoomDeviceAdapter;
import com.rdiot.yx485.bean.EditDeviceReply;
import com.rdiot.yx485.bean.RoomDeviceDetailBean;
import com.rdiot.yx485.databinding.ActivityRoomSetDeviceBinding;
import com.rdiot.yx485.util.DialogUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class RoomSetDeviceActivity extends BaseActivity {
    private final List<RoomDeviceDetailBean.RoomDeviceList.RoomDevice> mOutRoomDeviceList = new ArrayList<>();
    private final List<RoomDeviceDetailBean.RoomDeviceList.RoomDevice> mInRoomDeviceList = new ArrayList<>();
    private final EditDeviceReply mRoomInfo = new EditDeviceReply();
    private ActivityRoomSetDeviceBinding mBinding;
    private RoomViewModel mViewModel;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityRoomSetDeviceBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        initView();
        initData();
    }

    private void initData() {
        mViewModel = new ViewModelProvider(this).get(RoomViewModel.class);
        mViewModel.getErrorMessage().observe(this, this::showMessage);
        mViewModel.getRoomDeviceList().observe(this, list -> {
            mInRoomDeviceList.clear();
            if (list.in_room != null) {
                mInRoomDeviceList.addAll(list.in_room);
            }

            mOutRoomDeviceList.clear();
            if (list.out_room != null) {
                mOutRoomDeviceList.addAll(list.out_room);
            }

            refreshView();
        });
        mViewModel.getUpdateState().observe(this, success -> {
            if (success) {
                showMessage("保存成功");
                mViewModel.loadDeviceList(id);
            }
        });
        mViewModel.loadDeviceList(id);
    }

    private void initView() {
        mBinding.layoutTitle.tvTitle.setText("房间管理");
        mBinding.layoutTitle.btnTitleLeft.setOnClickListener(v -> finish());
        mBinding.layoutTitle.btnTitleRight.setText("保存");
        mBinding.layoutTitle.btnTitleRight.setOnClickListener(v -> {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                mRoomInfo.devices = mInRoomDeviceList
                        .stream()
                        .map(device -> new EditDeviceReply.DevicesBean(device.record_id, id))
                        .collect(Collectors.toList());
                mRoomInfo.devices.addAll(mOutRoomDeviceList
                        .stream().filter(device -> device.room_id.equals(id))
                        .map(device -> new EditDeviceReply.DevicesBean(device.record_id, ""))
                        .collect(Collectors.toList()));
            }
            mViewModel.upRoomInfo(id, mRoomInfo);
        });

        Intent intent = getIntent();
        id = intent.getStringExtra("roomid");
        String name = intent.getStringExtra("name");
        if (!TextUtils.isEmpty(name)) {
            mBinding.roomName.setText(name);
            mRoomInfo.name = name;
        }

        mBinding.roomName.setOnClickListener(v -> DialogUtils.showInputDialog(this, "房间名称", name, "请输入房间名称", roomName -> {
            mRoomInfo.name = roomName;
            mBinding.roomName.setText(roomName);
        }));

        mBinding.roomDevicesRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        RoomDeteleDeviceAdapter nonRoomAdapter = new RoomDeteleDeviceAdapter(mInRoomDeviceList, device -> {
            mInRoomDeviceList.remove(device);
            mOutRoomDeviceList.add(device);
            refreshView();
        });
        mBinding.roomDevicesRecyclerView.setAdapter(nonRoomAdapter);

        mBinding.nonRoomDevicesRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        RoomDeviceAdapter roomAdapter = new RoomDeviceAdapter(mOutRoomDeviceList, device -> {
            mOutRoomDeviceList.remove(device);
            mInRoomDeviceList.add(device);
            refreshView();
        });
        mBinding.nonRoomDevicesRecyclerView.setAdapter(roomAdapter);
    }

    @SuppressLint("NotifyDataSetChanged")
    private void refreshView() {
        mBinding.roomDevicesRecyclerView.getAdapter().notifyDataSetChanged();
        if (mInRoomDeviceList.isEmpty()) {
            mBinding.tvInRoomEmpty.setVisibility(View.VISIBLE);
        } else {
            mBinding.tvInRoomEmpty.setVisibility(View.GONE);
        }

        mBinding.nonRoomDevicesRecyclerView.getAdapter().notifyDataSetChanged();
        if (mOutRoomDeviceList.isEmpty()) {
            mBinding.tvOutRoomEmpty.setVisibility(View.VISIBLE);
        } else {
            mBinding.tvOutRoomEmpty.setVisibility(View.GONE);
        }
    }
}