package com.blocksolid.okrello.model;

import android.graphics.Color;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Dan Buckland on 02/12/2015.
 */
public class TrelloCard {

    private String id;
    private String name;
    private ArrayList<TrelloChecklist> checklists;

    public ArrayList<TrelloChecklist> getChecklists() {
        return checklists;
    }

    public void setChecklists(ArrayList<TrelloChecklist> checklists) {
        this.checklists = checklists;
    }


    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }



    public void setName(String name) {
        // TODO remove this after unit tests have been updated to use a mock backend
        this.name = name;
    }

    public String getScore() {
        Pattern pattern = Pattern.compile("\\[(\\d{1}\\.\\d{1})\\]");
        Matcher m = pattern.matcher(name);
        if (m.find()) {
            String score = m.group(1);
            double value = Double.parseDouble(score);
            if (value > 1) {
                return "1.0";
            } else {
                return score;
            }
        } else {
            return "!";
        }
    }

    public String getObjective() {
        return name.replaceAll("\\s?\\[\\d{1}\\.\\d{1}\\]", "").trim();
    }

    public int getScoreColor() {
        // No neat way to pull colors from colors.xml
        String grey = "#E6E6E6";
        String red = "#FF5722";
        String amber = "#FFC107";
        String green = "#8BC34A";

        String scoreString = getScore();
        int color = Color.parseColor(grey);
        if (!scoreString.equals("!")) {
            double score = Double.valueOf(scoreString);
            if (score < 0.25) {
                color = Color.parseColor(red);
            } else if (score < 0.55) {
                color = Color.parseColor(amber);
            } else {
                color = Color.parseColor(green);
            }
        }
        return color;
    }


}

@@ -2061,11 +2061,845 @@
 %0A    %7D%0A%0A
+    public int getKeyResultsChecklistPosition() %7B%0A        if (checklists.size() == 1) %7B%0A            // If there's only one checklist, use that checklist as Key Results%0A            return 0;%0A        %7D else if (checklists.size() %3E 1) %7B%0A            // When card has more than one checklist belonging to a card%0A            // only checkItems belonging to the checklist called %22Key Results%22 are displayed.%0A            for (int i = 0; i %3C checklists.size(); i++) %7B%0A                String checklistName = checklists.get(i).getName();%0A                if (checklistName.equals(%22Key Results%22)) %7B%0A                    return i;%0A                %7D%0A            %7D%0A        %7D%0A        // When there is no checklist called %22Key Results%22 belonging to a card with%0A        // multiple checklists, then no Key Results are displayed.%0A        return -1;%0A    %7D%0A
 %0A%7D%0A

Move getKeyResultsChecklistPosition method to TrelloCard class