package com.beem.project.beem.ui;

import org.jivesoftware.smack.packet.Presence;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.beem.project.beem.BeemApplication;
import com.beem.project.beem.R;
import com.beem.project.beem.service.Contact;
import com.beem.project.beem.service.PresenceAdapter;
import com.beem.project.beem.service.aidl.IXmppFacade;

public class ContactDialog extends Dialog {

    class aliasListener implements View.OnClickListener {

	@Override
	public void onClick(View v) {

	    ContactDialogAliasDialog dialog = new ContactDialogAliasDialog(mContext, mContact);
	    dialog.setOwnerActivity(ContactDialog.this.getOwnerActivity());
	    dialog.initService();
	    dialog.show();
	}

    }

    class chatListener implements View.OnClickListener {

	@Override
	public void onClick(View v) {
	    Activity a = ContactDialog.this.getOwnerActivity();
	    Intent i = new Intent(mContext, SendIM.class);
	    i.putExtra("contact", mContact);
	    a.startActivity(i);
	    dismiss();
	}

    }

    class groupListener implements View.OnClickListener {

	@Override
	public void onClick(View v) {
	    // TODO Auto-generated method stub
	    dismiss();
	}

    }

    class infosListener implements View.OnClickListener {

	@Override
	public void onClick(View v) {
	    // TODO Auto-generated method stub
	    dismiss();
	}

    }

    class resendListener implements View.OnClickListener {

	@Override
	public void onClick(View v) {
	    Presence presencePacket = new Presence(Presence.Type.subscribe);
	    presencePacket.setTo(mContact.getJID());
	    try {
		mService.sendPresencePacket(new PresenceAdapter(presencePacket));
	    } catch (RemoteException e) {
		Log.e(TAG, "resend subscription error", e);
	    }
	    dismiss();
	}

    }

    public static final String TAG = "Option Dialog";

    private Contact            mContact;

    private Context            mContext;

    private IXmppFacade        mService;

    public ContactDialog(final Context context, Contact curContact) {
	super(context);
	mContext = context;

	setContentView(R.layout.contactdialog);
	mContact = curContact;
	setTitle(curContact.getJID());

	Button chat = (Button) findViewById(R.id.CDChat);
	chat.setOnClickListener(new chatListener());
	Button alias = (Button) findViewById(R.id.CDAlias);
	alias.setOnClickListener(new aliasListener());
	Button group = (Button) findViewById(R.id.CDGroup);
	group.setOnClickListener(new groupListener());
	Button resend = (Button) findViewById(R.id.CDResend);
	resend.setOnClickListener(new resendListener());
	Button infos = (Button) findViewById(R.id.CDInfos);
	infos.setOnClickListener(new infosListener());
    }

    public void initService() {
	mService = BeemApplication.getApplication(ContactDialog.this.getOwnerActivity()).getXmppFacade();
    }

}
