package com.casic.dcms.widgets;

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.Calendar;
import java.util.Locale;

public class DetailsMarkerView extends MarkerView {

    private final DecimalFormat decimalFormat = new DecimalFormat("##0");
    private final TextView dayView;
    private final TextView dataView;

    public DetailsMarkerView(Context context) {
        super(context, R.layout.popu_marker);
        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();
        try {
            if (e.getY() == 0) {
                dataView.setText("暂无数据");
            } else {
                String type = "";
                if (data.equals("total")) {
                    type = "新增";
                } else {
                    type = "已处理";
                }
                dataView.setText(String.format(Locale.CHINA, "%s：%s件", type, decimalFormat.format(e.getY())));
            }
            Calendar calendar = Calendar.getInstance();
            int month = calendar.get(Calendar.MONTH) + 1;
            int day = (int) (e.getX() + 1);
            dayView.setText(String.format(Locale.CHINA, "%d月%s日", month, day));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        super.refreshContent(e, highlight);
    }

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