package com.casic.dcms.ui;

import android.content.Context;
import android.graphics.Rect;

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

import com.casic.dcms.R;
import com.casic.dcms.adapter.ProcessLineAdapter;
import com.casic.dcms.base.AndroidxBaseActivity;
import com.casic.dcms.bean.CaseProcessBean;
import com.casic.dcms.databinding.ActivityCaseProcessBinding;
import com.casic.dcms.mvp.presenter.impl.CaseProcessPresenterImpl;
import com.casic.dcms.mvp.view.ICaseProcessView;
import com.casic.dcms.utils.OtherUtils;
import com.casic.dcms.utils.TimeLineDecoration;
import com.casic.dcms.utils.TimeLineDecoration.TimeLineAdapter;
import com.casic.dcms.utils.ToastHelper;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;

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

public class CaseProcessActivity extends AndroidxBaseActivity<ActivityCaseProcessBinding> implements ICaseProcessView {

    private final Context context = CaseProcessActivity.this;
    private final List<CaseProcessBean.DataBean> dataBeans = new ArrayList<>();
    private CaseProcessPresenterImpl caseProcessPresenter;

    @Override
    protected void setupTopBarLayout() {
        viewBinding.titleBarLayout.titleView.setText("处理记录");
        viewBinding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        String processId = getIntent().getStringExtra("processId");
        caseProcessPresenter = new CaseProcessPresenterImpl(this);
        caseProcessPresenter.onReadyRetrofitRequest(processId);
    }

    @Override
    public void showProgress() {
        OtherUtils.showLoadingDialog(this, "获取案卷流程记录中，请稍后");
    }

    @Override
    public void obtainCaseProcessData(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));
            }
            ProcessLineAdapter adapter = new ProcessLineAdapter(this, dataBeans);
            viewBinding.processRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            viewBinding.processRecyclerView.setHasFixedSize(true);
            viewBinding.processRecyclerView.setNestedScrollingEnabled(false);
            viewBinding.processRecyclerView.setItemAnimator(new DefaultItemAnimator());
            TimeLineDecoration decoration = new TimeLineDecoration(this)
                    .setLineColor(R.color.lightGray)
                    .setLineWidth(1f)
                    .setLeftDistance(QMUIDisplayHelper.dp2px(this, 9))
                    .setMarkerColor(R.color.lightGray)
                    .setMarkerRadius(10f)
                    .setBeginMarkerRadius(10f)
                    .setBeginMarker(R.drawable.begin_marker)
                    .setCallback(new TimeLineAdapter() {
                        @Nullable
                        @Override
                        public Rect getRect(int position) {
                            return new Rect(QMUIDisplayHelper.dp2px(context, 30),
                                    QMUIDisplayHelper.dp2px(context, 10), 0, 0);
                        }

                        @Override
                        public int getTimeLineType(int position) {
                            if (position == 0) {
                                return TimeLineDecoration.BEGIN;
                            } else if (position == adapter.getItemCount() - 1) {
                                return TimeLineDecoration.END_FULL;
                            } else {
                                return TimeLineDecoration.NORMAL;
                            }
                        }
                    });
            viewBinding.processRecyclerView.addItemDecoration(decoration);
            viewBinding.processRecyclerView.setAdapter(adapter);
        }
    }

    @Override
    public void hideProgress() {
        OtherUtils.dismissLoadingDialog();
    }

    @Override
    public void obtainDataFail() {
        ToastHelper.showToast("获取案卷处理记录失败", ToastHelper.ERROR);
    }

    @Override
    protected void onDestroy() {
        caseProcessPresenter.disposeRetrofitRequest();
        super.onDestroy();
    }
}