package com.sjriley.zappit.models;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import android.content.Context;
import android.location.Location;
import com.google.gson.Gson;
import com.sjriley.zappit.vo.CreateAccountRequest;
import com.sjriley.zappit.vo.CreateAccountResponse;
import com.sjriley.zappit.vo.LoginRequest;
import com.sjriley.zappit.vo.LoginResponse;
public class CreateAccountModel extends BaseModel
{
public CreateAccountModel(Context context){
super(context);
}
public CreateAccountResponse createAccount(String code, String name, String email,
String password, String confirmpassword, String dobd, String dobm,
String doby, String gender, String postcode, String promo) {
HttpPost request = new HttpPost("http://zappit.co/service/register");
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
Location location = getLocation();
CreateAccountRequest params = new CreateAccountRequest(code, name,email,password,confirmpassword,dobd, dobm, doby,gender, postcode,promo,location.getLongitude(),location.getLatitude());
Gson gson = new Gson();
nvp.add(new BasicNameValuePair("info", gson.toJson(params)));
nvp.add(new BasicNameValuePair("submit", "send"));
CreateAccountResponse result = new CreateAccountResponse();
try
{
request.setEntity(new UrlEncodedFormEntity(nvp, HTTP.UTF_8));
HttpResponse response = httpClient.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
result = gson.fromJson(reader, CreateAccountResponse.class);
}
catch (UnsupportedEncodingException e)
{
result.setMessage("Could not encode parameters");
e.printStackTrace();
} catch (ClientProtocolException e)
{
result.setMessage("Could not connect to server");
e.printStackTrace();
} catch (IOException e)
{
result.setMessage("Could not read response");
e.printStackTrace();
}
catch (Exception e) {
result.setMessage("Could not create your account, please contact Zappit.");
e.printStackTrace();
}
return result;
}
}