package com.casic.dcms.view.streetlight;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.casic.dcms.R;
import com.casic.dcms.adapter.NormalRecyclerAdapter;
import com.casic.dcms.databinding.ActivityLampBoxManagerBinding;
import com.casic.dcms.model.ActionResultBean;
import com.casic.dcms.model.LampBoxDetailBean;
import com.casic.dcms.model.LampBoxListBean;
import com.casic.dcms.utils.ViewGroupKit;
import com.casic.dcms.vm.LampBoxViewModel;
import com.casic.dcms.widgets.LightDetailDialog;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.divider.RecyclerViewItemDivider;
import com.pengxh.androidx.lite.kit.ContextKit;
import com.pengxh.androidx.lite.kit.StringKit;
import com.pengxh.androidx.lite.utils.LoadState;
import com.pengxh.androidx.lite.utils.LoadingDialog;
import com.pengxh.androidx.lite.widget.TitleBarView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

public class LampBoxManagerActivity extends AndroidxBaseActivity<ActivityLampBoxManagerBinding> {

    private final Context context = this;
    private LampBoxViewModel lampBoxViewModel;
    private NormalRecyclerAdapter<LampBoxListBean.DataBean.RowsBean> adapter;
    private int page = 1;
    private boolean isRefresh, isLoadMore = false;
    private boolean isControlLampBox = false;

    @Override
    protected void setupTopBarLayout() {
        ViewGroupKit.initImmersionBar(binding.rootView, this, true, R.color.white);
        binding.titleView.setOnClickListener(new TitleBarView.OnClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {
                ContextKit.navigatePageTo(context, LampBoxOnMapActivity.class);
            }
        });
    }

    @Override
    protected void observeRequestState() {
        lampBoxViewModel.loadState.observe(this, new Observer<LoadState>() {
            @Override
            public void onChanged(LoadState loadState) {
                if (isControlLampBox) {
                    if (loadState == LoadState.Loading) {
                        LoadingDialog.show(LampBoxManagerActivity.this, "处理中，请稍后...");
                    } else {
                        LoadingDialog.dismiss();
                    }
                } else {
                    if (!isRefresh && !isLoadMore) {
                        if (loadState == LoadState.Loading) {
                            LoadingDialog.show(LampBoxManagerActivity.this, "数据加载中，请稍后...");
                        } else {
                            LoadingDialog.dismiss();
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        lampBoxViewModel = new ViewModelProvider(this).get(LampBoxViewModel.class);
        lampBoxViewModel.lampBoxResult.observe(this, new Observer<LampBoxListBean>() {
            @Override
            public void onChanged(LampBoxListBean resultBean) {
                if (resultBean.getCode() == 200) {
                    bindRecyclerView(resultBean);
                }
            }
        });
        lampBoxViewModel.getLampBoxesByPage(this, page);

        lampBoxViewModel.controlResult.observe(this, new Observer<ActionResultBean>() {
            @Override
            public void onChanged(ActionResultBean resultBean) {
                if (resultBean.getCode() == 200) {
                    isControlLampBox = false;
                    StringKit.show(context, "指令发送成功");
                }
            }
        });

        lampBoxViewModel.lampBoxDetailResult.observe(this, new Observer<LampBoxDetailBean>() {
            @Override
            public void onChanged(LampBoxDetailBean resultBean) {
                new LightDetailDialog.Builder().setContext(context).setLampBoxDetailBean(resultBean).build().show();
            }
        });
    }

    private void bindRecyclerView(LampBoxListBean resultBean) {
        List<LampBoxListBean.DataBean.RowsBean> dataRows = resultBean.getData().getRows();
        if (isRefresh) {
            binding.refreshLayout.finishRefresh();
            isRefresh = false;
            adapter.refresh(dataRows);
        } else if (isLoadMore) {
            binding.refreshLayout.finishLoadMore();
            isLoadMore = false;
            if (dataRows.size() == 0) {
                StringKit.show(context, "到底了，别拉了");
            }
            adapter.loadMore(dataRows);
        } else {
            if (dataRows.size() == 0) {
                binding.emptyView.show("没有路灯数据", null);
            } else {
                binding.emptyView.hide();
                adapter = new NormalRecyclerAdapter<LampBoxListBean.DataBean.RowsBean>(R.layout.item_lamp_box_rv_l, dataRows) {
                    @Override
                    public void convertView(ViewHolder viewHolder, int position, LampBoxListBean.DataBean.RowsBean item) {
                        viewHolder.setText(R.id.lampBoxNameView, item.getLampboxName())
                                .setText(R.id.lampBoxCodeView, item.getLampboxCode())
                                .setText(R.id.positionView, item.getStreetName())
                                .setOnClickListener(R.id.turnOffLampButton, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        isControlLampBox = true;
                                        lampBoxViewModel.controlLampBox(context, item.getId(), "0");
                                    }
                                })
                                .setOnClickListener(R.id.turnOnLampButton, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        isControlLampBox = true;
                                        lampBoxViewModel.controlLampBox(context, item.getId(), "1");
                                    }
                                });
                    }
                };
                binding.recyclerView.addItemDecoration(new RecyclerViewItemDivider(1, Color.LTGRAY));
                binding.recyclerView.setAdapter(adapter);
                adapter.setOnItemClickedListener(new NormalRecyclerAdapter.OnItemClickedListener<LampBoxListBean.DataBean.RowsBean>() {
                    @Override
                    public void onItemClicked(int position, LampBoxListBean.DataBean.RowsBean rowsBean) {
                        lampBoxViewModel.getLampBoxDetail(context, rowsBean.getId());
                    }
                });
            }
        }
    }

    @Override
    protected void initEvent() {
        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                isRefresh = true;
                //刷新之后页码重置
                page = 1;
                lampBoxViewModel.getLampBoxesByPage(context, page);
            }
        });
        binding.refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                isLoadMore = true;
                page++;
                lampBoxViewModel.getLampBoxesByPage(context, page);
            }
        });
    }
}