/*
 *  Nextcloud SingleSignOn
 *
 *  @author David Luhmer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.nextcloud.android.sso.aidl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NextcloudRequest implements Serializable {

    private static final long serialVersionUID = 215521212534240L; //assign a long value

    private String method;
    private Map<String, List<String>> header = new HashMap<>();
    private Map<String, String> parameter = new HashMap<>();
    private String requestBody;
    private String url;
    private String token;
    private String packageName;
    private String accountName;
    private boolean followRedirects;

    private NextcloudRequest() { }

    public static class Builder implements Serializable {

        private static final long serialVersionUID = 2121321432424242L; //assign a long value

        private NextcloudRequest ncr;

        public Builder() {
            ncr = new NextcloudRequest();
        }

        public NextcloudRequest build() {
            return ncr;
        }

        public Builder setMethod(String method) {
            ncr.method = method;
            return this;
        }

        public Builder setHeader(Map<String, List<String>> header) {
            ncr.header = header;
            return this;
        }

        public Builder setParameter(Map<String, String> parameter) {
            ncr.parameter = parameter;
            return this;
        }

        public Builder setRequestBody(String requestBody) {
            ncr.requestBody = requestBody;
            return this;
        }

        public Builder setUrl(String url) {
            ncr.url = url;
            return this;
        }

        public Builder setToken(String token) {
            ncr.token = token;
            return this;
        }

        public Builder setAccountName(String accountName) {
            ncr.accountName = accountName;
            return this;
        }

        /**
         * Default value: true
         * @param followRedirects
         * @return
         */
        public Builder setFollowRedirects(boolean followRedirects) {
            ncr.followRedirects = followRedirects;
            return this;
        }
    }

    public String getMethod() {
        return this.method;
    }

    public Map<String, List<String>> getHeader() {
        return this.header;
    }

    public Map<String, String> getParameter() {
        return this.parameter;
    }

    public String getRequestBody() {
        return this.requestBody;
    }

    public String getUrl() {
        return this.url;
    }

    public String getToken() {
        return this.token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getPackageName() {
        return this.packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getAccountName() {
        return this.accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public boolean isFollowRedirects() {
        return this.followRedirects;
    }


    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }

        if (!(obj instanceof NextcloudRequest)) {
            return false;
        }


        NextcloudRequest rq = (NextcloudRequest)obj;
        boolean equal;
        equal  = checkEqual("accountName", this.accountName, rq.accountName);
        equal &= checkEqual("header", this.header, rq.header);
        equal &= checkEqual("method", this.method, rq.method);
        equal &= checkEqual("packageName", this.packageName, rq.packageName);
        equal &= checkEqual("parameter", this.parameter, rq.parameter);
        equal &= checkEqual("requestBody", this.requestBody, rq.requestBody);
        equal &= checkEqual("token", this.token, rq.token);
        equal &= checkEqual("url", this.url, rq.url);
        equal &= checkEqual("followRedirects", this.followRedirects, rq.followRedirects);

        return equal;

        //return super.equals(obj);
    }

    private boolean checkEqual(String name, Object o1, Object o2) {
        if(o1 == null && o2 == null) {
            return true;
        }

        if(o1 != null) {
            boolean eq = o1.equals(o2);
            if(!eq) {
                System.err.println("[" + name + "] Expected: " + o1 + " Was: " + o2);
            }
            return eq;
        } else {
            // o1 == null and o2 != null
            System.err.println("[" + name + "] Expected: " + o1 + " Was: " + o2);
        }

        return false;
    }
}

@@ -847,16 +847,58 @@
 l.Map;%0A%0A
+import androidx.core.util.ObjectsCompat;%0A%0A
 public c
@@ -4189,34 +4189,29 @@
   = 
-checkEqual(%22accountName%22, 
+ObjectsCompat.equals(
 this
@@ -4262,29 +4262,29 @@
  &= 
-checkEqual(%22header%22, 
+ObjectsCompat.equals(
 this
@@ -4325,29 +4325,29 @@
  &= 
-checkEqual(%22method%22, 
+ObjectsCompat.equals(
 this
@@ -4388,34 +4388,29 @@
  &= 
-checkEqual(%22packageName%22, 
+ObjectsCompat.equals(
 this
@@ -4461,32 +4461,29 @@
  &= 
-checkEqual(%22parameter%22, 
+ObjectsCompat.equals(
 this
@@ -4530,34 +4530,29 @@
  &= 
-checkEqual(%22requestBody%22, 
+ObjectsCompat.equals(
 this
@@ -4603,28 +4603,29 @@
  &= 
-checkEqual(%22token%22, 
+ObjectsCompat.equals(
 this
@@ -4664,26 +4664,29 @@
  &= 
-checkEqual(%22url%22, 
+ObjectsCompat.equals(
 this
@@ -4721,38 +4721,29 @@
  &= 
-checkEqual(%22followRedirects%22, 
+ObjectsCompat.equals(
 this
@@ -4808,582 +4808,13 @@
 al;%0A
-%0A        //return super.equals(obj);%0A    %7D%0A%0A    private boolean checkEqual(String name, Object o1, Object o2) %7B%0A        if(o1 == null && o2 == null) %7B%0A            return true;%0A        %7D%0A%0A        if(o1 != null) %7B%0A            boolean eq = o1.equals(o2);%0A            if(!eq) %7B%0A                System.err.println(%22%5B%22 + name + %22%5D Expected: %22 + o1 + %22 Was: %22 + o2);%0A            %7D%0A            return eq;%0A        %7D else %7B%0A            // o1 == null and o2 != null%0A            System.err.println(%22%5B%22 + name + %22%5D Expected: %22 + o1 + %22 Was: %22 + o2);%0A        %7D%0A%0A        return false;%0A
     %7D
+%0A
 %0A%7D%0A

replace checkEqual with ObjectsCompat.equals