package beyondsoft.bewomitarbeiterapp;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.StackView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * Created by bib on 02.05.2016.
 */
public class KontaktChatActiviti extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //erstelle für Demo ein pic einmalig
     /*  ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Bitmap a =((BitmapDrawable)getResources().getDrawable(R.drawable.bslogo)).getBitmap();
         a.compress(Bitmap.CompressFormat.JPEG,0,stream);
        byte[] x = stream.toByteArray();
 */
        DatabaseHandler db = new DatabaseHandler(this);

        //Clear Database from Data
      /*  db.onUpgrade(db.getReadableDatabase(),1,2);
       //Add New ContactList
        db.addContact(new ContactList("Demo","1",x));
        db.addContact(new ContactList("Peter Fritz","2"));
        db.addContact(new ContactList("mattis Schlitz","3"));
        db.addContact(new ContactList("jan Detlef","2"));
        */


        List<ContactList> lc = db.getAllContacts();

        ContactList[] Klientel = lc.toArray(new ContactList[lc.size()]);

        setListAdapter(new KontaktAdapter(this,Klientel));
        db.close();
    }

//############### Menue bereiche ##########################
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main_activity_mitarbeiter, menu);
    return true;
}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement

        if (id == R.id.action_Logout) {
            System.exit(0);
        }

        return super.onOptionsItemSelected(item);
    }

//########################################################

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        //get selected items
        ContactList selectedValue = (ContactList) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue.getKontaktName(), Toast.LENGTH_SHORT).show();

        //

        Bundle korb = new Bundle();

        korb.putString("Name",selectedValue.getKontaktName());

        Intent in = new Intent(getBaseContext(), ChatActiviti.class);
        in.putExtras(korb);

        //gebe bild mit für den Cat
        if(selectedValue.getPicture() == null){
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            Bitmap a =  BitmapFactory.decodeResource(getResources(), R.drawable.katze3);
            a.compress(Bitmap.CompressFormat.JPEG,0,stream);
            byte[] x = stream.toByteArray();
            in.putExtra("Bild",x);
        }else
            in.putExtra("Bild",selectedValue.getPicture());



       startActivity(in);

    }







}
