/*************************************************************
 * By: Coralys Cubero Rivera
 * Date: 2019
 *************************************************************/

package uprrp.tania;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class SendResetPasswordToServer extends AsyncTask <String, String, String> {

    private Context context;

    public SendResetPasswordToServer(Context context){
        this.context = context.getApplicationContext();
    }

    @Override
    protected String doInBackground(String... strings) {
        String jsonResetPassword  = "";

        try {
            jsonResetPassword = URLEncoder.encode("data", "UTF-8") + "=" + URLEncoder.encode(strings[0], "UTF-8");

        }

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

        String serverReply = "";
        BufferedReader serverReader;

        //Send the signature to TANIA's server
        try
        {
            //Defined URL  where to send data
            URL url = new URL("https://tania.uprrp.edu/resetPassword.php");

            //Send POST data request
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);

            Writer writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
            writer.write(jsonResetPassword);
            writer.close();

            InputStream inputStream = conn.getInputStream();
            StringBuffer buffer = new StringBuffer();

            if (inputStream == null) {
                return null;
            }

            //Get the server response
            serverReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder serverResponse = new StringBuilder();
            String serverResponseLine;

            // Read Server Response
            while((serverResponseLine = serverReader.readLine()) != null)
            {
                //Append server response in string
                serverResponse.append(serverResponseLine);
            }

            serverReply = serverResponse.toString();
        }
        catch(Exception ex) {
            Log.e("ERROR RESET PASSWORD", ex.getMessage());
        }

        return serverReply;
    }

    @Override
    protected void onPostExecute(String s) {
        Log.d("RESET PASSWORD SERVER REPLY", s);

        String ss = s.substring(0,6);
        String se = s.substring(0,8);
        if (ss.equals("Succes")){

            try {
                Intent intent = new Intent(context, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
            catch (Exception e){
                Log.d("REGISTRATION TO MAIN ACTIVITY", e.getMessage());
            }

        }

        else if (se.equals("Error:fo")){
            Toast.makeText(context, "", Toast.LENGTH_LONG).show();
        }

        else {
            Toast.makeText(context, "An error occured while trying to register. Please start again.", Toast.LENGTH_LONG).show();
        }

    }
}
