package me.devsaki.hentoid.activities.bundles;

import android.net.Uri;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import me.devsaki.hentoid.database.domains.Attribute;
import me.devsaki.hentoid.enums.AttributeType;
import me.devsaki.hentoid.util.AttributeMap;

public class SearchActivityBundle {
    private static final String KEY_ATTRIBUTE_TYPES = "attributeTypes";
    private static final String KEY_MODE = "mode";
    private static final String KEY_URI = "uri";

    private SearchActivityBundle() {
        throw new UnsupportedOperationException();
    }

    public static final class Builder {

        private final Bundle bundle = new Bundle();

        public void setAttributeTypes(AttributeType... attributeTypes) {
            ArrayList<Integer> attrTypes = new ArrayList<>();
            for (AttributeType type : attributeTypes) attrTypes.add(type.getCode());

            bundle.putIntegerArrayList(KEY_ATTRIBUTE_TYPES, attrTypes);
        }

        public void setMode(int mode) {
            bundle.putInt(KEY_MODE, mode);
        }

        public Builder setUri(Uri uri) {
            bundle.putString(KEY_URI, uri.toString());
            return this;
        }

        public static Uri buildSearchUri(List<Attribute> attributes) {
            AttributeMap metadataMap = new AttributeMap();
            metadataMap.addAll(attributes);

            Uri.Builder searchUri = new Uri.Builder()
                    .scheme("search")
                    .authority("hentoid");
            for (Map.Entry<AttributeType,List<Attribute>> entry : metadataMap.entrySet()) {
                AttributeType attrType = entry.getKey();
                List<Attribute> list = entry.getValue();
                for (Attribute attr : list)
                    searchUri.appendQueryParameter(attrType.name(), attr.getId() + ";" + attr.getName());
            }
            return searchUri.build();
        }

        public Bundle getBundle() {
            return bundle;
        }
    }

    public static final class Parser {

        private final Bundle bundle;

        public Parser(@Nonnull Bundle bundle) {
            this.bundle = bundle;
        }

        public List<AttributeType> getAttributeTypes() {
            List<AttributeType> result = new ArrayList<>();

            List<Integer> attrTypesList = bundle.getIntegerArrayList(KEY_ATTRIBUTE_TYPES);
            if (null != attrTypesList && !attrTypesList.isEmpty())
                for (Integer i : attrTypesList) result.add(AttributeType.searchByCode(i));

            return result;
        }

        public int getMode() {
            return bundle.getInt(KEY_MODE, -1);
        }

        @Nullable
        public Uri getUri() {
            Uri result = null;

            String uriStr = bundle.getString(KEY_URI, "");
            if (!uriStr.isEmpty()) result = Uri.parse(uriStr);

            return result;
        }

        public static List<Attribute> parseSearchUri(Uri uri) {
            List<Attribute> result = new ArrayList<>();

            if (uri != null)
                for (String typeStr : uri.getQueryParameterNames()) {
                    AttributeType type = AttributeType.searchByName(typeStr);
                    if (type != null)
                        for (String attrStr : uri.getQueryParameters(typeStr)) {
                            String[] attrParams = attrStr.split(";");
                            if (2 == attrParams.length) {
                                result.add(new Attribute(type, attrParams[1]).setId(Long.parseLong(attrParams[0])));
                            }
                        }
                }

            return result;
        }
    }


}

@@ -146,30 +146,8 @@
 ist;
-%0Aimport java.util.Map;
 %0A%0Aim
@@ -1618,18 +1618,8 @@
 or (
-Map.Entry%3C
 Attr
@@ -1631,31 +1631,17 @@
 Type
-,List%3CAttribute%3E%3E entry
+ attrType
  : m
@@ -1651,20 +1651,18 @@
 dataMap.
+k
 e
-ntr
 ySet()) 
@@ -1671,32 +1671,37 @@
                 
+List%3C
 AttributeType at
@@ -1697,94 +1697,42 @@
 bute
-Type
+%3E
  attr
-Type = entry.getKey();%0A                List%3CAttribute%3E list = entry.getValue(
+s = metadataMap.get(attrType
 );%0A 
@@ -1772,12 +1772,13 @@
 r : 
-list
+attrs
 )%0A  

Rollback change from keySet() to entrySet()