package com.casic.dcms.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.TextView;

import com.casic.dcms.R;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Locale;

@SuppressLint("ViewConstructor")
public class DetailsMarkerView extends MarkerView {

    private final DecimalFormat decimalFormat = new DecimalFormat("##0");
    private final List<String> dates;
    private final TextView dayView;
    private final TextView dataView;

    public DetailsMarkerView(Context context, List<String> dates) {
        super(context, R.layout.popu_marker);
        this.dates = dates;
        dayView = findViewById(R.id.dayView);
        dataView = findViewById(R.id.dataView);
    }

    //每次重绘，会调用此方法刷新数据
    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        super.refreshContent(e, highlight);
        String data = (String) e.getData();
        int x = (int) e.getX();
        try {
            if (e.getY() == 0) {
                dataView.setText("暂无数据");
            } else {
                String type;
                switch (data) {
                    case "Registered":
                        type = "立案";
                        break;
                    case "Distribute":
                        type = "派遣";
                        break;
                    case "Process":
                        type = "处置";
                        break;
                    default:
                        type = "结案";
                        break;
                }
                dataView.setText(String.format(Locale.CHINA, "%s：%s件", type, decimalFormat.format(e.getY())));
            }
            dayView.setText(dates.get(x));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() >> 1), -getHeight());
    }
}
