package com.intfocus.template.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v4.util.ArrayMap;
import android.support.v4.util.SimpleArrayMap;
import android.view.View;

import com.intfocus.template.constant.Colors;
import com.intfocus.template.util.DisplayUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author zbaoliang
 * @date 17-5-21
 */
public class TableValueView extends View {

    private Paint dividerPaint;
    private Paint textPaint;
    private int[] colors = Colors.INSTANCE.getColorsRGY();

    public int dividerHeight = 1;
    public int textSize = 16;
    public int textColor = 0x73737373;
    public int dividerColor = 0x73737373;

    public void setItemHeight(int itemHeight) {
        this.itemHeight = itemHeight;
    }

    public void setHeaderLengths(ArrayList<Integer> headerLengths) {
        this.headerLenghts.clear();
        this.headerLenghts.addAll(headerLengths);
    }

    public void setTableValues(ArrayMap<Integer, String[]> lables) {
        this.tableValues.clear();
        tableValues.putAll((SimpleArrayMap<? extends Integer, ? extends String[]>) lables);
    }

    public void setDividerHeight(int dividerHeight) {
        this.dividerHeight = dividerHeight;
        dividerPaint.setStrokeWidth(dividerHeight);
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        textPaint.setColor(textColor);
        invalidate();
    }

    public void setDividerColor(int dividerColor) {
        this.dividerColor = dividerColor;
        dividerPaint.setColor(dividerColor);
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        textPaint.setTextSize(textSize);
        invalidate();
    }

    public TableValueView(Context context) {
        super(context);
        init();
    }

    private void init() {
        dividerPaint = new Paint();
        dividerPaint.setStyle(Paint.Style.FILL);
        dividerPaint.setAntiAlias(true);
        dividerPaint.setDither(true);
        dividerPaint.setColor(dividerColor);
        dividerPaint.setStrokeWidth(1f);

        textPaint = new Paint(dividerPaint);
        textPaint.setTextSize(DisplayUtil.sp2px(getContext(), textSize));
        textPaint.setTextAlign(Paint.Align.RIGHT);
//        textPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        textPaint.setColor(textColor);
        textPaint.setStrokeWidth(8);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int totalWidth = 0;
        for (Integer headerLength : headerLenghts) {
            totalWidth += headerLength;
        }
        setMeasuredDimension(totalWidth, tableValues.size() * (itemHeight + 1));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        initXYAxesTVCenter();
    }

    /**
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        try {
            canvas.drawColor(Color.WHITE);
            int nlSize = tableValues.size();
            float endX = getWidth();
            for (int i = 1; i < nlSize; i++) {
                float startY = itemHeight * (i + dividerHeight / 2) + (i + dividerHeight / 2);
                canvas.drawLine(0, startY, endX, startY, dividerPaint);
            }

            for (int i = 0; i < nlSize; i++) {
                String[] tabRowValues = tableValues.get(i);
                int headerSize = headerLenghts.size();
                Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
                float y = (int) (YAxesCenterPoint.get(i) - fontMetrics.top / 2 - fontMetrics.bottom / 2);
                for (int n = 0; n < headerSize; n++) {
                    float x = XAxesCenterPoint.get(n);
                    JSONObject rowData = new JSONObject(tabRowValues[n + 1]);
                    if (!"-1".equals(rowData.getString("color"))) {
                        textPaint.setColor(colors[Integer.parseInt(rowData.getString("color"))]);
                    } else {
                        textPaint.setColor(textColor);
                    }
                    String value = formatValue(rowData.getString("value"));
                    canvas.drawText(value, x, y, textPaint);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @NonNull
    private String formatValue(String value) {
        if (value == null || "".equals(value)) {
            value = "-";
        } else if (value.contains(".") && !value.contains("%")) {
            value = (value + "00").substring(0, value.indexOf(".") + 3);
        } else if (value.contains("%")) {
            value = (value.replace("%", "") + "00").substring(0, value.indexOf(".") + 3) + "%";
        }
        return value;
    }

    public int itemHeight;
    public ArrayList<Integer> headerLenghts = new ArrayList<>();
    public ArrayMap<Integer, String[]> tableValues = new ArrayMap<>();

    /**
     * X轴文本中心点
     */
    private ArrayMap<Integer, Float> XAxesCenterPoint = new ArrayMap<>();

    /**
     * Y轴文本中心点
     */
    private ArrayMap<Integer, Float> YAxesCenterPoint = new ArrayMap<>();

    /**
     * 计算XY轴中心点坐标
     */
    private void initXYAxesTVCenter() {
        XAxesCenterPoint.clear();
        YAxesCenterPoint.clear();

        //TODO 计算文本对应的X轴中心点
        int hlSize = headerLenghts.size();
        float upxPointLs = 0;
        for (int i = 0; i < hlSize; i++) {
            float width = upxPointLs + headerLenghts.get(i);

            float centerPwidth;
            centerPwidth = width - DisplayUtil.dip2px(getContext(), 10);
            XAxesCenterPoint.put(i, centerPwidth);

            upxPointLs = width;
        }

        //TODO 计算文本对应的Y轴中心点
        int nlSize = tableValues.size();
        float centerPHeight = -(float) (itemHeight / 2);
        for (int i = 0; i < nlSize; i++) {
            centerPHeight += itemHeight + 1f;
            YAxesCenterPoint.put(i, centerPHeight);
        }
    }
}
