package org.wikipedia.dataclient.mwapi;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.annotations.SerializedName;

import org.wikipedia.json.GsonUtil;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unused")
public class EditorTaskCounts {
    @Nullable private JsonElement counts;
    @Nullable @SerializedName("targets_passed") private JsonElement targetsPassed;
    @Nullable private JsonElement targets;

    @NonNull
    public Map<String, Integer> getDescriptionEditsPerLanguage() {
        Map<String, Integer> editsPerLanguage = null;
        if (counts != null && !(counts instanceof JsonArray)) {
            editsPerLanguage = GsonUtil.getDefaultGson().fromJson(counts, Counts.class).appDescriptionEdits;
        }
        return editsPerLanguage == null ? Collections.emptyMap() : editsPerLanguage;
    }

    @NonNull
    public List<Integer> getDescriptionEditTargetsPassed() {
        List<Integer> passedList = null;
        if (targetsPassed != null && !(targetsPassed instanceof JsonArray)) {
            passedList = GsonUtil.getDefaultGson().fromJson(targetsPassed, Targets.class).appDescriptionEdits;
        }
        return passedList == null ? Collections.emptyList() : passedList;
    }

    public int getDescriptionEditTargetsPassedCount() {
        List<Integer> targetList = getDescriptionEditTargets();
        List<Integer> passedList = getDescriptionEditTargetsPassed();
        int count = 0;
        if (!targetList.isEmpty() && !passedList.isEmpty()) {
            for (int target : targetList) {
                if (passedList.contains(target)) {
                    count++;
                }
            }
        }
        return count;
    }

    @NonNull
    public List<Integer> getDescriptionEditTargets() {
        List<Integer> targetList = null;
        if (targets != null && !(targets instanceof JsonArray)) {
            targetList = GsonUtil.getDefaultGson().fromJson(targets, Targets.class).appDescriptionEdits;
        }
        return targetList == null ? Collections.emptyList() : targetList;
    }

    @NonNull
    public Map<String, Integer> getCaptionEditsPerLanguage() {
        Map<String, Integer> editsPerLanguage = null;
        if (counts != null && !(counts instanceof JsonArray)) {
            editsPerLanguage = GsonUtil.getDefaultGson().fromJson(counts, Counts.class).appCaptionEdits;
        }
        return editsPerLanguage == null ? Collections.emptyMap() : editsPerLanguage;
    }

    @NonNull
    public List<Integer> getCaptionEditTargetsPassed() {
        List<Integer> passedList = null;
        if (targetsPassed != null && !(targetsPassed instanceof JsonArray)) {
            passedList = GsonUtil.getDefaultGson().fromJson(targetsPassed, Targets.class).appCaptionEdits;
        }
        return passedList == null ? Collections.emptyList() : passedList;
    }

    public int getCaptionEditTargetsPassedCount() {
        List<Integer> targetList = getCaptionEditTargets();
        List<Integer> passedList = getCaptionEditTargetsPassed();
        int count = 0;
        if (!targetList.isEmpty() && !passedList.isEmpty()) {
            for (int target : targetList) {
                if (passedList.contains(target)) {
                    count++;
                }
            }
        }
        return count;
    }

    @NonNull
    public List<Integer> getCaptionEditTargets() {
        List<Integer> targetList = null;
        if (targets != null && !(targets instanceof JsonArray)) {
            targetList = GsonUtil.getDefaultGson().fromJson(targets, Targets.class).appCaptionEdits;
        }
        return targetList == null ? Collections.emptyList() : targetList;
    }

    public class Counts {
        @Nullable @SerializedName("app_description_edits") private Map<String, Integer> appDescriptionEdits;
        @Nullable @SerializedName("app_caption_edits") private Map<String, Integer> appCaptionEdits;
    }

    public class Targets {
        @Nullable @SerializedName("app_description_edits") private List<Integer> appDescriptionEdits;
        @Nullable @SerializedName("app_caption_edits") private List<Integer> appCaptionEdits;
    }
}

@@ -1570,37 +1570,41 @@
 ();%0A        int 
-count
+maxPassed
  = 0;%0A        if
@@ -1605,66 +1605,161 @@
     
-if (!targetList.isEmpty() && !passedList.isEmpty()) %7B%0A    
+for (int passed : passedList) %7B%0A            if (passed %3E maxPassed) %7B%0A                maxPassed = passed;%0A            %7D%0A        %7D%0A        int count = 0;%0A
     
@@ -1810,51 +1810,34 @@
     
-    if (passedList.contains(
+if (maxPassed %3E 
 target)
-)
  %7B%0A
-    
     
@@ -1857,34 +1857,16 @@
 ount++;%0A
-                %7D%0A
         
@@ -3251,37 +3251,41 @@
 ();%0A        int 
-count
+maxPassed
  = 0;%0A        if
@@ -3286,66 +3286,161 @@
     
-if (!targetList.isEmpty() && !passedList.isEmpty()) %7B%0A    
+for (int passed : passedList) %7B%0A            if (passed %3E maxPassed) %7B%0A                maxPassed = passed;%0A            %7D%0A        %7D%0A        int count = 0;%0A
     
@@ -3491,51 +3491,34 @@
     
-    if (passedList.contains(
+if (maxPassed %3E 
 target)
-)
  %7B%0A
-    
     
@@ -3542,26 +3542,8 @@
 ++;%0A
-                %7D%0A
     

Fix tallying of suggested edit milestones.