package com.example.ismailamrani.comptable.ui;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ismailamrani.comptable.R;
import com.example.ismailamrani.comptable.ui.base.ColoredStatusBarActivity;
import com.example.ismailamrani.comptable.utils.JSONUtils;
import com.example.ismailamrani.comptable.utils.Method;
import com.example.ismailamrani.comptable.utils.RequestListener;
import com.example.ismailamrani.comptable.webservice.PhpAPI;
import com.squareup.picasso.Picasso;

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

import butterknife.ButterKnife;

/**
 * Created by Ismail Amrani on 25/03/2016.
 */
public class ProductDetailsActivity extends ColoredStatusBarActivity {
    ImageView Image;
    int id;

    TextView PrixHT, PrixTTC, Stock, Code, Libelle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.produit_info);
        ButterKnife.bind(this);

        setupActionBar();

        Image = (ImageView) findViewById(R.id.Imageaff);
        PrixHT = (TextView) findViewById(R.id.PrixHTaff);
        PrixTTC = (TextView) findViewById(R.id.PrixTTCaff);
        Code = (TextView) findViewById(R.id.Codeaff);
        Stock = (TextView) findViewById(R.id.Stockaff);
        Libelle = (TextView) findViewById(R.id.Libelleaff);

        Intent i = getIntent();
        id = i.getExtras().getInt("id");
        sendHTTPRequest(PhpAPI.getProduitById, JSONUtils.bundleIDToJSON(id), Method.POST,
                new RequestListener() {
                    @Override
                    public void onRequestSucceeded(JSONObject response, int status) {
                        if (status == 1) {
                            try {
                                JSONArray listproduits = response.getJSONArray("produit");

                                for (int i = 0; i < listproduits.length(); i++) {
                                    JSONObject usr = listproduits.getJSONObject(i);
                                    Picasso.with(getApplicationContext()).load(PhpAPI.IpBackend + "produits/" + usr.getString("photo")).into(Image);
                                    Libelle.setText(usr.getString("libelle"));
                                    PrixHT.setText(usr.getString("prixHT"));
                                    PrixTTC.setText(usr.getString("prixTTC"));
                                    Code.setText(usr.getString("codeBar"));
                                    Stock.setText(usr.getString("qte"));

                                    mActionBar.setTitle(usr.getString("libelle"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        } else if (status == 0) {
                            Toast.makeText(getApplicationContext(), "Produit Not Found  !!!!",
                                    Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onRequestFailed() {
                        runOnUiThread(() -> Toast.makeText(getApplicationContext(),
                                "Produit Not Found  !!!!", Toast.LENGTH_LONG).show());
                    }
                });
    }

    private void setupActionBar() {
        mActionBar.setActionBarListener(this);
    }

    @Override
    public void onAddPressed() {
        startActivity(new Intent(this, AddProductActivity.class));
    }
}

@@ -676,24 +676,49 @@
 SONObject;%0A%0A
+import butterknife.Bind;%0A
 import butte
@@ -864,84 +864,278 @@
 y %7B%0A
-    ImageView Image;%0A    int id;%0A%0A    TextView PrixHT, PrixTTC, Stock, Code,
+%0A    @Bind(R.id.Imageaff)%0A    ImageView Image;%0A    @Bind(R.id.PrixHTaff)%0A    TextView PrixHT;%0A    @Bind(R.id.PrixTTCaff)%0A    TextView PrixTTC;%0A    @Bind(R.id.Codeaff)%0A    TextView Code;%0A    @Bind(R.id.Stockaff)%0A    TextView Stock;%0A    @Bind(R.id.Libelleaff)%0A    TextView
  Lib
@@ -1362,25 +1362,24 @@
 nBar();%0A
-%0A
         
 Image = 
@@ -1374,392 +1374,28 @@
     
-Image = (ImageView) findViewById(R.id.Imageaff);%0A        PrixHT = (TextView) findViewById(R.id.PrixHTaff);%0A        PrixTTC = (TextView) findViewById(R.id.PrixTTCaff);%0A        Code = (TextView) findViewById(R.id.Codeaff);%0A        Stock = (TextView) findViewById(R.id.Stockaff);%0A        Libelle = (TextView) findViewById(R.id.Libelleaff);%0A%0A        Intent i = getIntent();%0A        id = i
+int id = getIntent()
 .get
@@ -1417,16 +1417,17 @@
 (%22id%22);%0A
+%0A
         

Use butterknife for view injections.