package com.casic.dcms.ui;

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

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.casic.dcms.R;
import com.casic.dcms.adapter.ProcessLineAdapter;
import com.casic.dcms.base.BaseActivity;
import com.casic.dcms.bean.CaseProcessBean;
import com.casic.dcms.mvp.presenter.CaseProcessPresenterImpl;
import com.casic.dcms.mvp.view.ICaseProcessView;
import com.casic.dcms.utils.TimeLineDecoration;
import com.casic.dcms.utils.ToastHelper;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

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

import butterknife.BindView;

public class CaseProcessActivity extends BaseActivity implements ICaseProcessView {

    private Context context = this;
    @BindView(R.id.caseTopLayout)
    QMUITopBarLayout caseTopLayout;
    @BindView(R.id.processRecyclerView)
    RecyclerView processRecyclerView;
    private CaseProcessPresenterImpl caseProcessPresenter;
    private QMUITipDialog loadingDialog;
    private List<CaseProcessBean.DataBean> dataBeans = new ArrayList<>();

    @Override
    public int initLayoutView() {
        return R.layout.activity_case_process;
    }

    @Override
    protected void setupTopBarLayout() {
        caseTopLayout.setTitle("处理记录").setTextColor(ContextCompat.getColor(this, R.color.white));
        caseTopLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.mainThemeColor));
        caseTopLayout.addLeftImageButton(R.drawable.ic_left_white, 1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        loadingDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("获取记录中，请稍后")
                .create();
    }

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

    @Override
    public void showProgress() {
        loadingDialog.show();
    }

    @Override
    public void obtainCaseProcessData(CaseProcessBean resultBean) {
        if (resultBean.isSuccess()) {
            //将数据逆序加载，便于查看
            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);
            processRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            processRecyclerView.setHasFixedSize(true);
            processRecyclerView.setNestedScrollingEnabled(false);
            processRecyclerView.setItemAnimator(new DefaultItemAnimator());
            TimeLineDecoration decoration = new TimeLineDecoration(this)
                    .setLineColor(R.color.lightGray)
                    .setLineWidth(1)
                    .setLeftDistance(QMUIDisplayHelper.dp2px(this, 9))
                    .setMarkerColor(R.color.lightGray)
                    .setMarkerRadius(10)
                    .setBeginMarkerRadius(10)
                    .setBeginMarker(R.drawable.begin_marker)
                    .setCallback(new TimeLineDecoration.TimeLineAdapter() {

                        @Override
                        public boolean isShowDivider(int position) {
                            return super.isShowDivider(position);
                        }

                        @Nullable
                        @Override
                        public Rect getRect(int position) {
                            //item内间距
                            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;
                        }
                    });
            processRecyclerView.addItemDecoration(decoration);
            processRecyclerView.setAdapter(adapter);
        }
    }

    @Override
    public void hideProgress() {
        loadingDialog.dismiss();
    }

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (caseProcessPresenter != null) {
            caseProcessPresenter.disposeRetrofitRequest();
        }
    }
}
