package com.epson.cameracopy.printlayout;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import java.io.Serializable;
import java.util.LinkedList;

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

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

        public DocumentSizeInfo[] newArray(int i) {
            return new DocumentSizeInfo[i];
        }
    };
    public static final int DOCSIZE_NAME_USER_DEFINE = 0;
    private static final int[] DOCUMENT_SIZE_NAME = {R.string.papersize_auto, R.string.papersize_auto, R.string.papersize_a3, R.string.papersize_a4, R.string.papersize_a5, R.string.papersize_a6, R.string.papersize_b4, R.string.papersize_b5, R.string.papersize_b6, R.string.papersize_letter, R.string.papersize_legal, R.string.papersize_postcard, R.string.papersize_10x15, R.string.papersize_l, R.string.papersize_2l, R.string.papersize_card, R.string.papersize_buzcard, R.string.papersize_passport};
    public static final double PAPER_A4_HEIGHT = 297.0d;
    public static final double PAPER_A4_WIDTH = 210.0d;
    public static final int PAPER_AUTO = -1;
    public static final double PAPER_LETTER_HEIGHT = 11.0d;
    public static final double PAPER_LETTER_WIDTH = 8.5d;
    public static final int PAPER_USER_DEFINE = -2;
    public static final int SCALE_TYPE_INCH = 2;
    public static final int SCALE_TYPE_MM = 1;
    private static final long serialVersionUID = 4845251233379966243L;
    private String mDocSizeName;
    private int mDocSizeNameId;
    private double mHeight;
    private int mPaperId;
    private int mScaleId;
    private double mWidth;

    public int describeContents() {
        return 0;
    }

    public DocumentSizeInfo() {
    }

    public String getDocSizeName() {
        return mDocSizeName;
    }

    public String getDocSizeName(Context context) {
        int i = mDocSizeNameId;
        if (i > 0) {
            int[] iArr = DOCUMENT_SIZE_NAME;
            if (i <= iArr.length - 1) {
                return context.getString(iArr[i]);
            }
        }
        return mDocSizeName;
    }

    public void setDocSizeName(String str) {
        mDocSizeName = str;
    }

    public int getScaleId() {
        return mScaleId;
    }

    public void setScaleId(int i) {
        mScaleId = i;
    }

    public double getWidth() {
        return mWidth;
    }

    public void setWidth(double d) {
        mWidth = d;
    }

    public double getHeight() {
        return mHeight;
    }

    public void setHeight(double d) {
        mHeight = d;
    }

    public int getPaperId() {
        return mPaperId;
    }

    public void setPaperId(int i) {
        mPaperId = i;
    }

    public int getDocSizeNameId() {
        return mDocSizeNameId;
    }

    public void setDocSizeNameId(int i) {
        mDocSizeNameId = i;
    }

    public void update(DocumentSizeInfo documentSizeInfo) {
        mDocSizeName = documentSizeInfo.mDocSizeName;
        mScaleId = documentSizeInfo.mScaleId;
        mWidth = documentSizeInfo.mWidth;
        mHeight = documentSizeInfo.mHeight;
        mDocSizeNameId = documentSizeInfo.mDocSizeNameId;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        DocumentSizeInfo documentSizeInfo = (DocumentSizeInfo) obj;
        String str = mDocSizeName;
        if (str == null) {
            if (documentSizeInfo.mDocSizeName != null) {
                return false;
            }
        } else if (!str.equals(documentSizeInfo.mDocSizeName)) {
            return false;
        }
        return true;
    }

    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(mDocSizeName);
        parcel.writeInt(mScaleId);
        parcel.writeDouble(mWidth);
        parcel.writeDouble(mHeight);
        parcel.writeInt(mPaperId);
        parcel.writeInt(mDocSizeNameId);
    }

    private DocumentSizeInfo(Parcel parcel) {
        mDocSizeName = parcel.readString();
        mScaleId = parcel.readInt();
        mWidth = parcel.readDouble();
        mHeight = parcel.readDouble();
        mPaperId = parcel.readInt();
        mDocSizeNameId = parcel.readInt();
    }

    public static LinkedList<DocumentSizeInfo> getDocumentSizeInfoList(JSONObject jSONObject) throws JSONException {
        LinkedList<DocumentSizeInfo> linkedList = new LinkedList<>();
        JSONArray jSONArray = jSONObject.getJSONArray("DocumentSizeInfoList");
        for (int i = 0; i < jSONArray.length(); i++) {
            DocumentSizeInfo documentSizeInfo = new DocumentSizeInfo();
            try {
                JSONObject jSONObject2 = jSONArray.getJSONObject(i);
                documentSizeInfo.mDocSizeName = jSONObject2.getString("mDocSizeName");
                documentSizeInfo.mScaleId = jSONObject2.getInt("mScaleId");
                documentSizeInfo.mWidth = jSONObject2.getDouble("mWidth");
                documentSizeInfo.mHeight = jSONObject2.getDouble("mHeight");
                documentSizeInfo.mPaperId = jSONObject2.getInt("mPaperId");
                documentSizeInfo.mDocSizeNameId = jSONObject2.getInt("mDocSizeNameId");
                linkedList.add(documentSizeInfo);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return linkedList;
    }

    public static JSONArray makeDocumentSizeInfoList(LinkedList<DocumentSizeInfo> linkedList) throws JSONException {
        JSONArray jSONArray = new JSONArray();
        for (int i = 0; i < linkedList.size(); i++) {
            DocumentSizeInfo documentSizeInfo = linkedList.get(i);
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.put("mDocSizeName", documentSizeInfo.mDocSizeName);
                jSONObject.put("mScaleId", documentSizeInfo.mScaleId);
                jSONObject.put("mWidth", documentSizeInfo.mWidth);
                jSONObject.put("mHeight", documentSizeInfo.mHeight);
                jSONObject.put("mPaperId", documentSizeInfo.mPaperId);
                jSONObject.put("mDocSizeNameId", documentSizeInfo.mDocSizeNameId);
                jSONArray.put(jSONObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jSONArray;
    }
}
