package ssrmarcsautelet.automation.appli.ssrmarcsautelet;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by Robin on 26/04/2017.
 */

public class activity_val extends AppCompatActivity {

    public int DIALOG_DOWNLOAD_PROGRESS = 1;
    private ProgressDialog progressDialog;
    public String url = "http://192.168.43.197/documents/";
    public EditText saisie;
    public Button validation;
    public Context context;
    public int duration = Toast.LENGTH_SHORT;
    public String recup_mot;

    public static File CreateFile() {
        File path = new File(Environment.getExternalStorageDirectory(), "/SSR_docs/");
        path.mkdirs();
        return path;
    }

    String[] dir = {
            "contact_map.html",
            "fiche_amb1.html",
            "fiche_amb2.html",
            "fiche_amb3.html",
            "fiche_ane1.html",
            "fiche_ane2.html",
            "fiche_ane3.html",
            "fiche_assist1.html",
            "fiche_assist2.html",
            "fiche_assist3.html",
            "fiche_educ1.html",
            "fiche_educ2.html",
            "fiche_educ3.html",
            "fiche_ergo1.html",
            "fiche_ergo2.html",
            "fiche_ergo3.html",
            "fiche_inf1.html",
            "fiche_inf2.html",
            "fiche_inf3.html",
            "fiche_kine1.html",
            "fiche_kine2.html",
            "fiche_kine3.html",
            "fiche_medecin1.html",
            "fiche_medecin2.html",
            "fiche_medecin3.html",
            "fiche_ortho1.html",
            "fiche_ortho2.html",
            "fiche_ortho3.html",
            "fiche_orthoptiste1.html",
            "fiche_orthoptiste2.html",
            "fiche_orthoptiste3.html",
            "fiche_psycho1.html",
            "fiche_psycho2.html",
            "fiche_psycho3.html",
            "fiche_secret1.html",
            "fiche_secret2.html",
            "fiche_secret3.html",
            "google_map.html",
            "orga_accueil.html",
            "orga_apa.html",
            "orga_appareilleur.html",
            "orga_assist.html",
            "orga_cadre.html",
            "orga_doc.html",
            "orga_ecole",
            "orga_ergo.html",
            "orga_grands.html",
            "orga_inf.html",
            "orga_kine.html",
            "orga_moyens.html",
            "orga_ortho.html",
            "orga_orthoptiste.html",
            "orga_petits.html",
            "orga_psy.html",
            "orga_psychomo.html",
            "orga_secret.html",
            "orga_standard.html",
            "Ambulancier.jpg",
            "Kine.jpg",
            "ane.jpg",
            "assistante.jpg",
            "educateur.jpg",
            "ergo.jpg",
            "inf.jpg",
            "medecin.jpg",
            "ortho.jpg",
            "orthoptiste.jpg",
            "psychologue.jpg",
            "secretaire.jpg",
            "accueil1.png",
            "accueil2.png",
            "accueil3.png",
            "accueil4.png",
            "apa1.png",
            "appareillage1.png",
            "assist1.png",
            "dame1.png",
            "dame2.png",
            "doc1.png",
            "doc2.png",
            "ecole1.png",
            "ecole2.png",
            "ergo1.png",
            "ergo2.png",
            "ergo3.png",
            "ergo4.png",
            "ergo5.png",
            "grands1.png",
            "grands2.png",
            "grands3.png",
            "inf1.png",
            "inf2.png",
            "inf3.png",
            "kine1.png",
            "kine10.png",
            "kine11.png",
            "kine12.png",
            "kine2.png",
            "kine3.png",
            "kine4.png",
            "kine5.png",
            "kine6.png",
            "kine7.png",
            "kine8.png",
            "kine9.png",
            "moyens1.png",
            "moyens2.png",
            "moyens3.png",
            "moyens4.png",
            "moyens5.png",
            "ortho1.png",
            "ortho2.png",
            "ortho3.png",
            "orthopto1.png",
            "pediatre1.png",
            "petits1.png",
            "petits2.png",
            "petits3.png",
            "petits4.png",
            "psy1.png",
            "psy2.png",
            "psy3.png",
            "psychomo1.png",
            "secret1.png",
            "secret2.png",
            "Ambulancier.jpg",
            "Kine.jpg",
            "ane.jpg",
            "assistante.jpg",
            "educateur.jpg",
            "ergo.jpg",
            "inf.jpg",
            "medecin.jpg",
            "ortho.jpg",
            "orthoptiste.jpg",
            "psychologue.jpg",
            "secretaire.jpg",
            "accueil1.png",
            "accueil2.png",
            "accueil3.png",
            "accueil4.png",
            "apa1.png",
            "appareillage1.png",
            "assist1.png",
            "dame1.png",
            "dame2.png",
            "doc1.png",
            "doc2.png",
            "ecole1.png",
            "ecole2.png",
            "ergo1.png",
            "ergo2.png",
            "ergo3.png",
            "ergo4.png",
            "ergo5.png",
            "grands1.png",
            "grands2.png",
            "grands3.png",
            "inf1.png",
            "inf2.png",
            "inf3.png",
            "kine1.png",
            "kine10.png",
            "kine11.png",
            "kine12.png",
            "kine2.png",
            "kine3.png",
            "kine4.png",
            "kine5.png",
            "kine6.png",
            "kine7.png",
            "kine8.png",
            "kine9.png",
            "moyens1.png",
            "moyens2.png",
            "moyens3.png",
            "moyens4.png",
            "moyens5.png",
            "ortho1.png",
            "ortho2.png",
            "ortho3.png",
            "orthopto1.png",
            "pediatre1.png",
            "petits1.png",
            "petits2.png",
            "petits3.png",
            "petits4.png",
            "psy1.png",
            "psy2.png",
            "psy3.png",
            "psychomo1.png",
            "secret1.png",
            "secret2.png"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.motdepasse);
        saisie = (EditText) findViewById(R.id.texte_mdp);
        validation = (Button) findViewById(R.id.valid);
        validation.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                if (saisie.getText().toString().equals("projetima4"))
                {
                    CreateFile();
                    new activity_val.DownloadFileAsync().execute(
                            url + dir[0], url + dir[1], url + dir[2], url + dir[3], url + dir[4], url + dir[5],
                            url + dir[6], url + dir[7], url + dir[8], url + dir[9], url + dir[10], url + dir[11],
                            url + dir[12], url + dir[13], url + dir[14], url + dir[15], url + dir[16], url + dir[17],
                            url + dir[18], url + dir[19], url + dir[20], url + dir[21], url + dir[22],
                            url + dir[23], url + dir[24], url + dir[25], url + dir[26], url + dir[27], url + dir[28], url + dir[29],
                            url + dir[30], url + dir[31], url + dir[32], url + dir[33], url + dir[34], url + dir[35], url + dir[36],
                            url + dir[37], url + dir[38], url + dir[39], url + dir[40], url + dir[41], url + dir[42], url + dir[43],
                            url + dir[44], url + dir[45], url + dir[46], url + dir[47], url + dir[48], url + dir[49], url + dir[50],
                            url + dir[51], url + dir[52], url + dir[53], url + dir[54], url + dir[55], url + dir[56], url + dir[57], url + dir[58],
                            url + dir[59], url + dir[60], url + dir[61], url + dir[62], url + dir[63], url + dir[64],
                            url + dir[65], url + dir[66], url + dir[67], url + dir[68], url + dir[69], url + dir[70],
                            url + dir[71], url + dir[72], url + dir[73], url + dir[74], url + dir[75], url + dir[76],
                            url + dir[77], url + dir[78], url + dir[79], url + dir[80], url + dir[81], url + dir[82], url + dir[83],
                            url + dir[84], url + dir[85], url + dir[86], url + dir[87], url + dir[88], url + dir[89], url + dir[90],
                            url + dir[91], url + dir[92], url + dir[93], url + dir[94], url + dir[95], url + dir[96], url + dir[97],
                            url + dir[98], url + dir[99], url + dir[100], url + dir[101], url + dir[102], url + dir[103], url + dir[104],
                            url + dir[105], url + dir[106], url + dir[107], url + dir[108], url + dir[109], url + dir[110], url + dir[111], url + dir[112],
                            url + dir[113], url + dir[114], url + dir[115], url + dir[116], url + dir[117], url + dir[118],
                            url + dir[119], url + dir[120], url + dir[121], url + dir[122],url + dir[123], url + dir[124], url + dir[125], url + dir[126], url + dir[127],
                            url + dir[128], url + dir[129], url + dir[130], url + dir[131], url + dir[132], url + dir[133],
                            url + dir[134], url + dir[135], url + dir[136], url + dir[137], url + dir[138], url + dir[139],
                            url + dir[140], url + dir[141], url + dir[142], url + dir[143], url + dir[144],
                            url + dir[145], url + dir[146], url + dir[147], url + dir[148], url + dir[149], url + dir[150], url + dir[151],
                            url + dir[152], url + dir[153], url + dir[154], url + dir[155], url + dir[156], url + dir[157], url + dir[158],
                            url + dir[159], url + dir[160], url + dir[161], url + dir[162], url + dir[163], url + dir[164], url + dir[165],
                            url + dir[166], url + dir[167], url + dir[168], url + dir[169], url + dir[170], url + dir[171], url + dir[172],
                            url + dir[173], url + dir[174], url + dir[175], url + dir[176], url + dir[177], url + dir[178], url + dir[179], url + dir[180],
                            url + dir[181], url + dir[182], url + dir[183], url + dir[184], url + dir[185], url + dir[186],
                            url + dir[187], url + dir[188], url + dir[189], url + dir[190], url + dir[191]);//

                    context = getApplicationContext();
                    Toast toast1 = Toast.makeText(context, "Téléchargement en cours", duration);
                    toast1.show();
                } else {
                    context = getApplicationContext();
                    Toast toast2 = Toast.makeText(context, "Mot de passe inconnu", duration);
                    toast2.show();
                }

            }
        });
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch (id) {
            case 1:
                progressDialog = new ProgressDialog(this);
                progressDialog.setMessage("Téléchargement des fichiers");
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setCancelable(false);
                progressDialog.show();
                return progressDialog;
            default:
                return null;
        }
    }

    class DownloadFileAsync extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            int count;
            for (int i = 0; i < dir.length; i++) {
                try {
                    URL url = new URL(params[i]);
                    URLConnection connexion = url.openConnection();
                    connexion.connect();
                    int lengthofFile = connexion.getContentLength();
                    Log.d("ANDRO_ASYNC", "Length of file: " + lengthofFile);
                    InputStream input = new BufferedInputStream(url.openStream());
                    OutputStream output = new FileOutputStream("/sdcard/SSR_docs/" + dir[i]);
                    byte data[] = new byte[1024];
                    long total = 0;
                    while ((count = input.read(data)) != -1) {
                        total += count;
                        publishProgress("" + (int) ((total * 100) / lengthofFile));
                        output.write(data, 0, count);
                    }
                    output.flush();
                    output.close();
                    input.close();
                } catch (Exception e) {
                    Log.i("FAIL", "Problème de téléchargement");
                    // TODO: handle exception
                }

            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        @Override
        protected void onProgressUpdate(String... values) {
            // TODO Auto-generated method stub
            Log.d("ANDRO_ASYNC", values[0]);
            progressDialog.setProgress(Integer.parseInt(values[0]));
        }
    }

   }

