package pl.helion.javaee7.klientogloszen;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Date;


public class AddNewActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_new);
    }


    @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_add_new, 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_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void dodaj(View view) {
        String tytul = pobierzTekst(R.id.editTytul);
        String tresc = pobierzTekst(R.id.editTresc);
        String autor = pobierzTekst(R.id.editAutor);

        String json = new Ogloszenie(1, tytul, autor, new Date(), tresc).toJSON();
        new ZadanieHttpDodanieNowego().execute("POST", "http://10.3.1.7:8080/SerwerOgloszen/webresources/ogloszenie/", json);
    }

    public void potwierdzNowego(String wynik) {
        //Toast.makeText(getBaseContext(), wynik, Toast.LENGTH_LONG).show();
    }


    private String pobierzTekst(int id) {
        EditText edt = (EditText)findViewById(id);
        return edt.getText().toString();
    }

    class ZadanieHttpDodanieNowego extends ZadanieHttp {
        @Override
        protected void onPostExecute(String wynik) {
            potwierdzNowego(wynik);
        }
    }
}
