package com.lake.tahoe.activities;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.lake.tahoe.R;
import com.lake.tahoe.callbacks.ModelCallback;
import com.lake.tahoe.models.Request;
import com.lake.tahoe.models.User;
import com.lake.tahoe.utils.ActivityUtil;
import com.lake.tahoe.utils.PushUtil;
import com.parse.ParsePush;
public class RequestActiveVendorActivity extends RequestActiveActivity implements
ModelCallback<Request> {
ImageView ivCheck;
ImageView ivCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
User.getCurrentUser().getUnfinishedRequest(this);
ivCheck = (ImageView) findViewById(R.id.ivCheck);
ivCheck.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
completeRequest();
}
});
ivCancel = (ImageView) findViewById(R.id.ivCancel);
ivCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
abortRequest();
}
});
}
@Override
public void onModelFound(Request _request) {
request = _request;
createViews(request.getClient());
createMapViews(request.getClient());
String title = String.format("%s | %s", request.getDisplayDollars(), request.getTitle());
getDynamicActionBar().setTitle(title);
getDynamicActionBar().setRightArrowAction(new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityUtil.startRequestDetailActivity(RequestActiveVendorActivity.this, request);
}
});
}
public void abortRequest() {
request.setVendor(null);
request.setState(Request.State.OPEN);
request.saveAndPublish(new PushUtil.HandlesPublish() {
@Override public void onPublished(ParsePush push) {
finish();
}
@Override public void onError(Throwable t) {
RequestActiveVendorActivity.this.onError(t);
}
});
finish();
}
@Override
public void finish() {
super.finish();
ActivityUtil.transitionLeft(this);
}
public void completeRequest() {
toggleBlocker(true);
request.setState(Request.State.PENDING);
request.saveAndPublish(new PushUtil.HandlesPublish() {
@Override public void onPublished(ParsePush push) {
ActivityUtil.startRequestPendingActivity(RequestActiveVendorActivity.this, User.getCurrentUser());
ActivityUtil.transitionFade(RequestActiveVendorActivity.this);
}
@Override public void onError(Throwable t) {
RequestActiveVendorActivity.this.onError(t);
}
});
}
@Override
public void onModelError(Throwable t) {
onError(t);
}
@Override
public void onLocationChanged(Location location) {
super.onLocationChanged(location);
if (request != null) {
updateUserDistance(User.getCurrentUser(), request.getClient());
}
}
@Override
public void onUserUpdated(User user) {
if (user == null || request == null)
return;
if (user.getObjectId().equals(request.getClient().getObjectId())) {
request.setClient(user);
updateUserDistance(User.getCurrentUser(), user);
updateRemoteUserMarker(user);
}
}
@Override
public void onUserUpdateError(Throwable t) {
onError(t);
}
@Override
protected void onGooglePlayServicesError(Throwable t) {
onError(t);
}
@Override
protected void onLocationTrackingFailed(Throwable t) {
onError(t);
}
}