package com.rdiot.yx485.home;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.rdiot.yx485.R;
import com.rdiot.yx485.adapter.HomeAddRoomtAdapter;
import com.rdiot.yx485.base.BaseFragment;
import com.rdiot.yx485.bean.CreateHomeBean;
import com.rdiot.yx485.bean.DefHomeRoomBean;
import com.rdiot.yx485.databinding.ActivityCreatHomeBinding;
import com.rdiot.yx485.util.DialogUtils;

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

public class HomeCreateFragment extends BaseFragment {
    private final HomeViewModel mViewModel = new HomeViewModel();
    private HomeAddRoomtAdapter adapter;
    private ActivityCreatHomeBinding mBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = ActivityCreatHomeBinding.inflate(inflater, container, false);
        initView();
        initData();
        return mBinding.getRoot();
    }

    private void initView() {
        mBinding.addroom.setOnClickListener(view -> showAddRoomDialog());
        mBinding.addHomeButton.setOnClickListener(view -> CreateHome());
        mBinding.homeadress.setOnClickListener(view -> {
            hideSoftKeyBroad();
            if (getActivity() instanceof HomeCreateActivity) {
                ((HomeCreateActivity) getActivity()).selectAddress();
            }
        });
    }

    private void initData() {
        mViewModel.getDefaultRoomList().observe(getViewLifecycleOwner(), this::setupRecyclerView);
        mViewModel.loadDefaultRoomList();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getActivity() instanceof HomeCreateActivity) {
            String address = ((HomeCreateActivity) getActivity()).getAddress();
            if (TextUtils.isEmpty(address)) {
                return;
            }
            mBinding.homeadress.setText(address);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mBinding.homeadress.setTextColor(requireContext().getColor(R.color.black_33));
            }
        }
    }

    /**
     * 创建home
     */
    private void CreateHome() {
        String name = mBinding.homename.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showMessage("请输入名称");
        }
        List<CreateHomeBean.RoomBean> room = new ArrayList<>();
        if (adapter != null) {
            List<DefHomeRoomBean.DataBean.CityInfo> list = adapter.getdate();
            for (int i = 0; i < list.size(); i++) {
                CreateHomeBean.RoomBean bean = new CreateHomeBean.RoomBean();
                bean.name = list.get(i).name;
                room.add(bean);
            }
        }

        CreateHomeBean bean = new CreateHomeBean();
        bean.room = room;
        bean.name = name;
        if (getActivity() instanceof HomeCreateActivity) {
            ((HomeCreateActivity) getActivity()).createHome(bean);
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private void setupRecyclerView(List<DefHomeRoomBean.DataBean.CityInfo> data) {
        adapter = new HomeAddRoomtAdapter(data, position -> {
            data.remove(position);
            adapter.notifyDataSetChanged();
        });
        mBinding.roomRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        mBinding.roomRecyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    private void showAddRoomDialog() {
        DialogUtils.showInputDialog(requireActivity(), "添加房间", null, "请输入房间名称", name -> {
            // 处理添加房间逻辑
            DefHomeRoomBean.DataBean.CityInfo bean = new DefHomeRoomBean.DataBean.CityInfo();
            bean.name = name;
            List<DefHomeRoomBean.DataBean.CityInfo> data = adapter.getdate();
            data.add(bean);
            adapter.setdate(data);
            adapter.notifyDataSetChanged();
        });
    }
}
