package es.ua.eps.filmoteca;

import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class FilmEditActivity extends AppCompatActivity {

    private static final String TAG = "FilmEditActivity";

    public static final String EXTRA_FILM_INDEX = "EXTRA_FILM_INDEX";

    int mFilmIndex;

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

        mFilmIndex = getIntent().getIntExtra(EXTRA_FILM_INDEX, -1);

        Button btnGuardar = (Button)findViewById(R.id.btnEditSave);
        Button btnCancelar = (Button)findViewById(R.id.btnEditCancel);

        btnGuardar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveFilmData();
                setResult(RESULT_OK);
                Log.d(TAG, "Datos guardados");
                finish();
            }
        });

        btnCancelar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                Log.d(TAG, "Edición cancelada");
                finish();
            }
        });

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        loadFilmData();
    }

    private void loadFilmData() {
        EditText etTitulo = (EditText) findViewById(R.id.etEditTitle);
        TextView etDirector = (TextView) findViewById(R.id.etEditDirector);
        TextView etAnyo = (TextView) findViewById(R.id.etEditYear);
        TextView etComentarios = (TextView) findViewById(R.id.etEditComments);
        TextView etImdb = (TextView) findViewById(R.id.etEditImdb);
        Spinner spinnerFormato = (Spinner) findViewById(R.id.spinnerEditFormat);
        Spinner spinnerGenero = (Spinner) findViewById(R.id.spinnerEditGenre);
        ImageView imgPoster = (ImageView) findViewById(R.id.imgEditPoster);

        Film film = FilmDataSource.films.get(mFilmIndex);

        etTitulo.setText(film.title);
        etDirector.setText(film.director);
        etAnyo.setText("" + film.year);
        etComentarios.setText(film.comments);
        etImdb.setText(film.imdbUrl);
        spinnerFormato.setSelection(film.format);
        spinnerGenero.setSelection(film.genre);
        imgPoster.setImageResource(film.imageResId);
    }

    private void saveFilmData() {
        EditText etTitulo = (EditText) findViewById(R.id.etEditTitle);
        TextView etDirector = (TextView) findViewById(R.id.etEditDirector);
        TextView etAnyo = (TextView) findViewById(R.id.etEditYear);
        TextView etComentarios = (TextView) findViewById(R.id.etEditComments);
        TextView etImdb = (TextView) findViewById(R.id.etEditImdb);
        Spinner spinnerFormato = (Spinner) findViewById(R.id.spinnerEditFormat);
        Spinner spinnerGenero = (Spinner) findViewById(R.id.spinnerEditGenre);
        ImageView imgPoster = (ImageView) findViewById(R.id.imgEditPoster);

        Film film = FilmDataSource.films.get(mFilmIndex);

        film.title = etTitulo.getText().toString();
        film.director = etDirector.getText().toString();
        film.year = Integer.parseInt(etAnyo.getText().toString());
        film.comments = etComentarios.getText().toString();
        film.imdbUrl = etImdb.getText().toString();
        film.genre = spinnerGenero.getSelectedItemPosition();
        film.format = spinnerFormato.getSelectedItemPosition();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpTo(this, new Intent(this, FilmListFragment.class));
                return true;
        }
        return false;
    }
}
