package com.example.nuria.fesp_cyl;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;

import com.example.nuria.fesp_cyl.DataBase.DbHelper;
import com.example.nuria.fesp_cyl.DataBase.StatusContract;


public class NoticiasFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{

    private static final String TAG = NoticiasFragment.class.getSimpleName();
    private SimpleCursorAdapter mAdapter;
    private static final String[] FROM = {StatusContract.Column.TITULO,
            StatusContract.Column.FECHA, StatusContract.Column.CATEGORIA};
    private static final int[] TO = {R.id.titulo, R.id.fecha, R.id.categoria};
    private static final int LOADER_ID = 42;
    String nuevaCat;
    Cursor c;


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setEmptyText("Sin datos...");


        mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.item_lista,null, FROM, TO, 0);
        setListAdapter(mAdapter);

        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG, "onItemClick");

                Cursor cursor = (Cursor)getListAdapter().getItem(position);

                Intent intent = new Intent(getActivity(), DetailActivity.class);
                String des = cursor.getString(cursor.getColumnIndex(StatusContract.Column.DESCRIPCION));
                des = des.replaceAll("<img.*/>", " [imagen] ");
                des = des.replaceAll("<a.*</a>", " [documento] ");


                Bundle bundle = new Bundle();
                bundle.putString(StatusContract.Column.TITULO, cursor.getString(cursor.getColumnIndex(StatusContract.Column.TITULO)));
                bundle.putString(StatusContract.Column.FECHA, cursor.getString(cursor.getColumnIndex(StatusContract.Column.FECHA)));
                bundle.putString(StatusContract.Column.DESCRIPCION, des);

                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        getLoaderManager().initLoader(LOADER_ID, null, this);

    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        if (i != LOADER_ID)
            return null;
        Log.d(TAG, "onCreateLoader");
        return new CursorLoader(getActivity(), StatusContract.CONTENT_URI, null, null,
                null, StatusContract.DEFAULT_SORT);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        Log.d(TAG, "onLoadFinished with cursor: " + cursor.getCount());
        mAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

        mAdapter.swapCursor(null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //you can set the title for your toolbar here for different fragments different titles
        getActivity().setTitle("FeSP_CyL: Educación");


    }

}
