package epson.print.copy.Component.ecopycomponent;

import android.os.Parcel;
import android.os.Parcelable;

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

import java.util.ArrayList;
import java.util.Iterator;

import epson.print.copy.Component.eremoteoperation.ERemoteCopy;

public class ECopyOptionContext implements Parcelable {
    public static final Parcelable.Creator<ECopyOptionContext> CREATOR = new Parcelable.Creator<ECopyOptionContext>() {
        public ECopyOptionContext createFromParcel(Parcel parcel) {
            return new ECopyOptionContext(parcel);
        }

        public ECopyOptionContext[] newArray(int i) {
            return new ECopyOptionContext[i];
        }
    };
    ERemoteCopy.ERemoteCopyOptionsResult copyOptionsResult;
    ECopyComponent.ECopyType copyType;
    ArrayList<ECopyOptionItem> optionItems;

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(this.copyType.name());
        parcel.writeInt(this.optionItems.size());
        Iterator<ECopyOptionItem> it = optionItems.iterator();
        while (it.hasNext()) {
            parcel.writeParcelable(it.next(), 0);
        }
        parcel.writeString(this.copyOptionsResult.toString());
    }

    ECopyOptionContext(Parcel parcel) {
        optionItems = new ArrayList<>();
        copyType = ECopyComponent.ECopyType.valueOf(parcel.readString());
        int readInt = parcel.readInt();
        for (int i = 0; i < readInt; i++) {
            optionItems.add((ECopyOptionItem) parcel.readParcelable(ECopyOptionItem.class.getClassLoader()));
        }
        try {
            JSONObject jSONObject = new JSONObject(parcel.readString());
            ERemoteCopy eRemoteCopy = new ERemoteCopy();
            eRemoteCopy.getClass();
            copyOptionsResult = new ERemoteCopy.ERemoteCopyOptionsResult(jSONObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private boolean isNull() {
        return copyType == null;
    }

    ECopyOptionContext() {
        optionItems = new ArrayList<>();
        copyType = null;
    }

    ECopyOptionContext(ECopyComponent.ECopyType eCopyType) {
        optionItems = new ArrayList<>();
        copyType = eCopyType;
    }

    ECopyOptionContext(ECopyOptionContext eCopyOptionContext) {
        optionItems = new ArrayList<>();
        copyType = null;
        if (eCopyOptionContext != null) {
            copyType = eCopyOptionContext.copyType;
            Iterator<ECopyOptionItem> it = eCopyOptionContext.optionItems.iterator();
            while (it.hasNext()) {
                optionItems.add(new ECopyOptionItem(it.next()));
            }
            copyOptionsResult = eCopyOptionContext.copyOptionsResult;
        }
    }

    private ECopyComponent.ECopyType getCopyType() {
        return copyType;
    }

    private ArrayList<ECopyOptionItem> getCopyOptionItems() {
        return optionItems;
    }

    private ECopyOptionItem getCopyOptionItemOf(ECopyOptionItem.ECopyOptionItemKey eCopyOptionItemKey) {
        Iterator<ECopyOptionItem> it = optionItems.iterator();
        while (it.hasNext()) {
            ECopyOptionItem next = it.next();
            if (next.key == eCopyOptionItemKey) {
                return next;
            }
        }
        return null;
    }

    private void add(ECopyOptionItem eCopyOptionItem) {
        optionItems.add(eCopyOptionItem);
    }

    public ECopyOptionItem replace(ECopyOptionItem eCopyOptionItem) {
        Iterator<ECopyOptionItem> it = optionItems.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (it.next().key == eCopyOptionItem.key) {
                return optionItems.set(i, eCopyOptionItem);
            }
            i++;
        }
        return null;
    }

    private boolean isChanged(ECopyOptionItem eCopyOptionItem) {
        Iterator<ECopyOptionItem> it = optionItems.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            ECopyOptionItem next = it.next();
            if (next.key == eCopyOptionItem.key) {
                if (!next.equals(eCopyOptionItem)) {
                    return true;
                }
            }
        }
        return false;
    }

    private void setCopyOptionsResult(ERemoteCopy.ERemoteCopyOptionsResult eRemoteCopyOptionsResult) {
        copyOptionsResult = eRemoteCopyOptionsResult;
    }

    private ERemoteCopy.ERemoteCopyOptionsResult getCopyOptionsResult() {
        return copyOptionsResult;
    }
}
