package es.ua.eps.filmoteca;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class FilmDataFragment extends Fragment {

    public static final String EXTRA_FILM_INDEX = "EXTRA_FILM_INDEX";

    private static final int REQUEST_CODE_EDIT = 1;

    int mFilmIndex = -1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_film_data, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mFilmIndex = getArguments().getInt(EXTRA_FILM_INDEX, -1);

        Button btnImdb = (Button)getActivity().findViewById(R.id.btnImdb);
        Button btnEditar = (Button)getActivity().findViewById(R.id.btnDataEditar);
        Button btnVolver = (Button)getActivity().findViewById(R.id.btnDataVolver);

        btnImdb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.imdb.com/title/tt0088763/?ref_=nv_sr_1"));
                startActivity(intent);
            }
        });

        btnEditar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), FilmEditActivity.class);
                intent.putExtra(FilmEditActivity.EXTRA_FILM_INDEX, mFilmIndex);
                startActivityForResult(intent, REQUEST_CODE_EDIT);
            }
        });

        updateFilmData();
    }

    private void updateFilmData() {
        TextView txtTitulo = (TextView) getActivity().findViewById(R.id.txtDataTitle);
        TextView txtAnyo = (TextView) getActivity().findViewById(R.id.txtDataYear);
        TextView txtDirector = (TextView) getActivity().findViewById(R.id.txtDataDirector);
        TextView txtComentarios = (TextView) getActivity().findViewById(R.id.txtDataComments);
        TextView txtFormatoGenero = (TextView) getActivity().findViewById(R.id.txtDataFormatGenre);
        ImageView imgPoster = (ImageView) getActivity().findViewById(R.id.imgDataPoster);

        if(mFilmIndex >= 0) {
            Film film = FilmDataSource.films.get(mFilmIndex);

            txtTitulo.setText(film.title);
            txtDirector.setText(film.director);
            txtAnyo.setText("" + film.year);
            txtComentarios.setText(film.comments);
            txtFormatoGenero.setText(getResources().getStringArray(R.array.formats)[film.format] +
                    ", " + getResources().getStringArray(R.array.genres)[film.genre]);
            imgPoster.setImageResource(film.imageResId);
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_CODE_EDIT && resultCode == Activity.RESULT_OK) {
            updateFilmData();
        }
    }

    public void setFilm(int position) {
        mFilmIndex = position;
        updateFilmData();
    }
}
