package com.example.ismailamrani.comptable.models;

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

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

/**
 * Created by Ismail Amrani on 23/03/2016.
 */
public class Product {

    int ID;
    String Libelle;
    Double PrixHT;
    Double PrixTTC;
    String CodeBarre;
    String Photo;
    int Qte;
    int Locale_ID;
    String url;

    /**
     * Overloaded constructor.
     * Creates a new Product model based on the passed-in JSON object
     *
     * @param object to be parsed.
     */
    public Product(JSONObject object) throws JSONException {
        this(
                object.getInt("idp"),
                object.getString("libelle"),
                object.getDouble("PrixHT"),
                object.getDouble("PrixTTC"),
                object.getString("codeBar"),
                object.getString("photo"),
                object.getInt("qte"),
                object.getInt("local"),
                ""
        );
    }

    public Product(int ID, String libelle, Double prixHT, Double prixTTC,
                   String codeBarre, String photo, int qte, int locale_ID, String url) {
        this.ID = ID;
        Libelle = libelle;
        PrixHT = prixHT;
        PrixTTC = prixTTC;
        CodeBarre = codeBarre;
        Photo = photo;
        Qte = qte;
        Locale_ID = locale_ID;
        this.url = url;
    }

    public Product() {}

    /**
     * @param array of products in JSON format
     * @return list of parsed products
     */
    public static List<Product> parseProducts(JSONArray array) {
        List<Product> products = new ArrayList<>();

        for (int i = 0; i < array.length(); i++) {
            try {
                JSONObject product = array.getJSONObject(i);
                products.add(new Product(product));

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return products;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getLibelle() {
        return Libelle;
    }

    public void setLibelle(String libelle) {
        Libelle = libelle;
    }

    public Double getPrixHT() {
        return PrixHT;
    }

    public void setPrixHT(Double prixHT) {
        PrixHT = prixHT;
    }

    public Double getPrixTTC() {
        return PrixTTC;
    }

    public void setPrixTTC(Double prixTTC) {
        PrixTTC = prixTTC;
    }

    public String getCodeBarre() {
        return CodeBarre;
    }

    public void setCodeBarre(String codeBarre) {
        CodeBarre = codeBarre;
    }

    public String getPhoto() {
        return Photo;
    }

    public void setPhoto(String photo) {
        Photo = photo;
    }

    public int getQte() {
        return Qte;
    }

    public void setQte(int qte) {
        Qte = qte;
    }

    public int getLocale_ID() {
        return Locale_ID;
    }

    public void setLocale_ID(int locale_ID) {
        Locale_ID = locale_ID;
    }
}

@@ -259,24 +259,32 @@
 duct %7B%0A%0A    
+private 
 int ID;%0A    
@@ -279,24 +279,32 @@
 int ID;%0A    
+private 
 String Libel
@@ -307,24 +307,32 @@
 ibelle;%0A    
+private 
 Double PrixH
@@ -338,16 +338,24 @@
 HT;%0A    
+private 
 Double P
@@ -362,24 +362,32 @@
 rixTTC;%0A    
+private 
 String CodeB
@@ -392,24 +392,32 @@
 eBarre;%0A    
+private 
 String Photo
@@ -418,24 +418,32 @@
  Photo;%0A    
+private 
 int Qte;%0A   
@@ -443,16 +443,24 @@
 te;%0A    
+private 
 int Loca
@@ -466,24 +466,32 @@
 ale_ID;%0A    
+private 
 String url;%0A
@@ -2053,32 +2053,612 @@
 roducts;%0A    %7D%0A%0A
+%0A    /**%0A     * @return a JSON object representing this Product object.%0A     */%0A    public JSONObject toJSON() %7B%0A        JSONObject object = new JSONObject();%0A        try %7B%0A            object.put(%22Libelle%22, Libelle);%0A            object.put(%22PrixHT%22, PrixHT);%0A            object.put(%22PrixTTC%22, PrixTTC);%0A            object.put(%22CodeBar%22, CodeBarre);%0A            object.put(%22Qte%22, Qte);%0A            object.put(%22Photo%22, Photo);%0A            object.put(%22Local%22, Locale_ID);%0A%0A        %7D catch (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        return object;%0A    %7D%0A%0A
     public Strin

Add a 'toJSON' method to the Product class.