package com.casic.dcms.view.bridge;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
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.ActivityMaintainRecordBinding;
import com.casic.dcms.model.MaintainRecordBean;
import com.casic.dcms.utils.ViewGroupKit;
import com.casic.dcms.vm.BridgeViewModel;
import com.casic.dcms.widgets.RangeDateActionSheet;
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.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 MaintainRecordActivity extends AndroidxBaseActivity<ActivityMaintainRecordBinding> {
    private final Context context = this;
    private BridgeViewModel bridgeViewModel;
    private NormalRecyclerAdapter<MaintainRecordBean.DataBean.RowsBean> adapter;
    private int page = 1;
    private boolean isRefresh = false;
    private boolean isLoadMore = 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() {

            }
        });
    }

    @Override
    protected void observeRequestState() {
        bridgeViewModel.loadState.observe(this, new Observer<LoadState>() {
            @Override
            public void onChanged(LoadState loadState) {
                if (!isRefresh && !isLoadMore) {
                    if (loadState == LoadState.Loading) {
                        LoadingDialog.show(MaintainRecordActivity.this, "查询记录中，请稍后");
                    } else {
                        LoadingDialog.dismiss();
                    }
                }
            }
        });
    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        bridgeViewModel = new ViewModelProvider(this).get(BridgeViewModel.class);
        bridgeViewModel.recordModel.observe(this, new Observer<MaintainRecordBean>() {
            @Override
            public void onChanged(MaintainRecordBean resultBean) {
                if (resultBean.getCode() == 200) {
                    bindRecyclerView(resultBean);

                }
            }
        });
        getMaintainRecord("", "");
    }

    private void bindRecyclerView(MaintainRecordBean resultBean) {
        List<MaintainRecordBean.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<MaintainRecordBean.DataBean.RowsBean>(R.layout.item_maintain_rv_l, dataRows) {
                    @Override
                    public void convertView(ViewHolder viewHolder, int position, MaintainRecordBean.DataBean.RowsBean item) {
                        String description = item.getDescription();
                        if (TextUtils.isEmpty(description)) {
                            viewHolder.setVisibility(R.id.descriptionImageView, View.GONE);
                        } else {
                            viewHolder.setVisibility(R.id.descriptionImageView, View.VISIBLE);
                            viewHolder.setOnClickListener(R.id.descriptionImageView, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    StringKit.show(context, description);
                                }
                            });
                        }
                        viewHolder.setText(R.id.bridgeNameView, item.getBridgeName())
                                .setText(R.id.maintainContentView, item.getContent())
                                .setText(R.id.maintainTimeView, item.getMaintainTimeStr());
                    }
                };
                binding.recyclerView.addItemDecoration(new RecyclerViewItemDivider(1, Color.LTGRAY));
                binding.recyclerView.setAdapter(adapter);
            }
        }
    }

    @Override
    protected void initEvent() {
        binding.calendarView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new RangeDateActionSheet.Builder().setContext(context).setOnActionSheetListener(new RangeDateActionSheet.OnDateRangeSelectedListener() {
                    @Override
                    public void onDateRangeSelected(String startDate, String endDate) {
                        binding.dateView.setText(startDate + "~" + endDate);

                        page = 1;
                        getMaintainRecord(startDate, endDate);
                    }
                }).build().show();
            }
        });
        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isRefresh = true;
                //刷新之后页码重置
                page = 1;
                getMaintainRecord("", "");
            }
        });
        binding.refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadMore = true;
                page++;
                getMaintainRecord("", "");
            }
        });
    }

    private void getMaintainRecord(String start, String end) {
        bridgeViewModel.getMaintainRecord(this, start, end, page);
    }
}