package com.sendsms.Parsing;

import android.util.Log;
import com.sendsms.model.MessageDetails;
import com.sendsms.model.UserDetails;
import com.sendsms.utills.Constant;

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

import java.util.ArrayList;

public class MessageParsing {


    public static Object[] getAllMessageDetails(String url) {
        try {
            ArrayList<MessageDetails> messageDetailsArray = new ArrayList<MessageDetails>();

            JSONObject parameters = new JSONObject();

            Log.d("Request", parameters.toString());

            JSONObject json = ServerCommunication.callJson(url, parameters);

            Log.d("Response", json.toString());

            JSONArray schoolArr = json.getJSONArray("sms_list");


            if (schoolArr != null) {
                for (int i = 0; i < schoolArr.length(); i++) {
                    JSONObject jsonObject = schoolArr.getJSONObject(i);
                    MessageDetails messageDetailsBean = new MessageDetails();
                    messageDetailsBean.setId(jsonObject.getString("id"));
                    messageDetailsBean.setName(jsonObject.getString("client_name"));
                    messageDetailsBean.setNumber(jsonObject.getString("mobile"));
                    messageDetailsBean.setMessage(jsonObject.getString("message"));
                   /* messageDetailsBean.setPhoto(jsonObject.getString("photo"));*/

                    messageDetailsArray.add(messageDetailsBean);
                }
                return new Object[]{true,messageDetailsArray};
            }
            return new Object[]{false, "Sorry, Connection Failed!"};

        } catch (Exception e) {
            e.printStackTrace();
            return new Object[]{false, "Sorry, Connection Failed!"};
        }
    }


    public static Object[] getAllUserDetails() {
        try {
            ArrayList<UserDetails> userDetailsArray = new ArrayList<UserDetails>();

            JSONObject parameters = new JSONObject();

            Log.d("Request", parameters.toString());

            JSONObject json = ServerCommunication.callJson(Constant.USER_URL, parameters);

            Log.d("Response", json.toString());

            JSONArray schoolArr = json.getJSONArray("user_list");


            if (schoolArr != null) {
                for (int i = 0; i < schoolArr.length(); i++) {
                    JSONObject jsonObject = schoolArr.getJSONObject(i);
                    UserDetails userDetailsBean = new UserDetails();
                    userDetailsBean.setId(jsonObject.getString("id"));
                    userDetailsBean.setName(jsonObject.getString("name"));
                    userDetailsBean.setPassword(jsonObject.getString("password"));
                    userDetailsBean.setL_login(jsonObject.getString("l_login"));
                    userDetailsBean.setCaption(jsonObject.getString("caption"));
                    userDetailsBean.setApi_base_url(jsonObject.getString("api_base_url"));
                    userDetailsBean.setApi_link_path(jsonObject.getString("api_link_path"));
                    userDetailsBean.setApi_name(jsonObject.getString("api_name"));

                    userDetailsArray.add(userDetailsBean);
                }
                return new Object[]{true,userDetailsArray};
            }
            return new Object[]{false, "Sorry, Connection Failed!"};

        } catch (Exception e) {
            e.printStackTrace();
            return new Object[]{false, "Sorry, Connection Failed!"};
        }
    }


}
