package com.casic.dcms.view;

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

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.ActivityCaseProcessBinding;
import com.casic.dcms.model.CaseProcessBean;
import com.casic.dcms.utils.ViewGroupKit;
import com.casic.dcms.vm.CommonDataViewModel;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.utils.Constant;
import com.pengxh.androidx.lite.utils.LoadState;
import com.pengxh.androidx.lite.utils.LoadingDialog;

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

public class CaseProcessActivity extends AndroidxBaseActivity<ActivityCaseProcessBinding> {

    private final List<CaseProcessBean.DataBean> dataBeans = new ArrayList<>();
    private CommonDataViewModel commonDataViewModel;

    @Override
    protected void setupTopBarLayout() {
        ViewGroupKit.initImmersionBar(binding.rootView, this, true, R.color.white);

        binding.titleBarLayout.titleView.setText("处理记录");
        binding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void observeRequestState() {
        commonDataViewModel.loadState.observe(this, new Observer<LoadState>() {
            @Override
            public void onChanged(LoadState loadState) {
                if (loadState == LoadState.Loading) {
                    LoadingDialog.show(CaseProcessActivity.this, "获取数据中...");
                } else {
                    LoadingDialog.dismiss();
                }
            }
        });
    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        String processId = getIntent().getStringExtra(Constant.INTENT_PARAM_KEY);
        commonDataViewModel = new ViewModelProvider(this).get(CommonDataViewModel.class);
        commonDataViewModel.getCaseProcess(this, processId);
        commonDataViewModel.caseProcessModel.observe(this, new Observer<CaseProcessBean>() {
            @Override
            public void onChanged(CaseProcessBean resultBean) {
                if (resultBean.getCode() == 200) {
                    //将数据逆序加载，便于查看
                    int size = resultBean.getData().size();
                    for (int i = 0; i < size; i++) {
                        dataBeans.add(i, resultBean.getData().get(size - 1 - i));
                    }
                    NormalRecyclerAdapter<CaseProcessBean.DataBean> recyclerAdapter = new NormalRecyclerAdapter<CaseProcessBean.DataBean>(R.layout.item_case_process, dataBeans) {
                        @Override
                        public void convertView(ViewHolder viewHolder, int position, CaseProcessBean.DataBean item) {
                            if (position == 0) {
                                //最后一项
                                viewHolder.setBackgroundColor(R.id.dotView, Color.BLACK);
                                viewHolder.setTextColor(R.id.operatorNameView, Color.BLACK)
                                        .setTextColor(R.id.statusView, Color.BLACK)
                                        .setTextColor(R.id.operateTimeView, Color.BLACK)
                                        .setTextColor(R.id.remarkView, Color.BLACK);

                                viewHolder.setVisibility(R.id.topLineView, View.INVISIBLE);
                                viewHolder.setImageResource(R.id.tagImageView, R.drawable.ic_dot_red);
                            } else if (position == size - 1) {
                                viewHolder.setVisibility(R.id.bottomLineView, View.INVISIBLE);
                                viewHolder.setImageResource(R.id.tagImageView, R.drawable.ic_dot_gray);
                            } else {
                                viewHolder.setImageResource(R.id.tagImageView, R.drawable.ic_dot);
                            }

                            viewHolder.setText(R.id.statusView, item.getOperationTypeName())
                                    .setText(R.id.operatorNameView, item.getUserName())
                                    .setText(R.id.remarkView, item.getRemarks())
                                    .setText(R.id.operateTimeView, item.getTime());
                        }
                    };
                    binding.processRecyclerView.setAdapter(recyclerAdapter);
                }
            }
        });
    }

    @Override
    protected void initEvent() {

    }
}