package com.application.tak.takapplication.data_access;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.application.tak.takapplication.data_model.Category;
import com.application.tak.takapplication.data_model.Class;
import com.application.tak.takapplication.data_model.Class_V;
import com.application.tak.takapplication.data_model.School;

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

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by azielinska on 16/11/2017.
 */

public class GetClassBySignupCode extends GetJSONData {
    private static final String URL = Config.SERVER_NAME +"get_class_by_signup_code.php";
    private static final String TAG_RESULTS="class";
    private static final String TAG_ID="ID";
    private static final String TAG_CLASS_NAME="CLASS_NAME";
    private static final String TAG_SCHOOL_ID="SCHOOL_ID";
    private static final String TAG_SIGNUP_CODE="SIGNUP_CODE";
    private static final String TAG_SCHOOL_NAME="SCHOOL_NAME";
    private static final String TAG_ADRESS_ID="ADRESS_ID";
    JSONArray classes = null;

    public Class_V _class;
    public DB_result dbResult = null;

    public GetClassBySignupCode(Context ctx, Integer signupcode)
    {
        super(ctx);
        showProgressDialog = false;
        execute(new String[]{signupcode.toString()});

    }

    @Override
    protected String doInBackground(String... params)
    {
        String result ="";
        String signup_code = params[0];

        BufferedReader bufferedReader = null;
        try {
            java.net.URL url = new URL(URL);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);

            OutputStream os=con.getOutputStream();

            String data=
                    URLEncoder.encode("signup_code","UTF-8")+"="+URLEncoder.encode(signup_code,"UTF-8")+"&"+
                            URLEncoder.encode("application_id","UTF-8")+"="+URLEncoder.encode(Config.ApplicationId,"UTF-8");

            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
            bufferedWriter.write(data);
            bufferedWriter.flush();

            StringBuilder sb = new StringBuilder();

            bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

            int statusCode = con.getResponseCode();
            if(statusCode == 200) {
                String json;
                while ((json = bufferedReader.readLine()) != null) {
                    sb.append(json + "\n");
                }
                result = sb.toString().trim();

                if(ProcessData(result))
                {
                    Helper returnData = new Helper();
                    returnData.ProcessData(json);
                    dbResult = returnData.resultset;
                }
            }
            return result;
        }
        catch(Exception e)
        {
            Log.d("ERROR", e.toString());
            return "";
        }

    }


    boolean ProcessData(String json)
    {
        boolean isOk = false;
        JSONObject jsonObject;
        try
        {
            jsonObject = new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));

            classes = jsonObject.getJSONArray(TAG_RESULTS);

            for(int i=0;i<classes.length();i++)
            {
                if(i == 0)
                {
                    _class = new Class_V();
                    School school = new School();
                    JSONObject c = classes.getJSONObject(i);
                    _class.set_Id(c.getInt(TAG_ID));
                    _class.set_SchoolId(c.getInt(TAG_SCHOOL_ID));;
                    _class.set_SignUpCode(c.getInt(TAG_SIGNUP_CODE));
                    _class.set_ClassName(c.isNull(TAG_CLASS_NAME)? "": c.getString(TAG_CLASS_NAME));
                    school.set_SchoolName(c.isNull(TAG_SCHOOL_NAME)? "":c.getString(TAG_SCHOOL_NAME));
                    school.set_AdressId(c.getInt(TAG_ADRESS_ID));
                    _class.set_School(school);

                    isOk = true;
                }
            }
        }
        catch (Exception ex)
        {
            Log.d("ERROR", ex.toString());
        }

        return isOk;
    }
}
