package fr.utc.assos.uvweb;

import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import fr.utc.assos.uvweb.adapters.UVCommentAdapter;
import fr.utc.assos.uvweb.data.UVwebContent;

/**
 * A fragment representing a single UV detail screen. This fragment is either
 * contained in a {@link fr.utc.assos.uvweb.activities.UVListActivity} in two-pane mode (on tablets) or a
 * {@link fr.utc.assos.uvweb.activities.UVDetailActivity} on handsets.
 */
public class UVDetailFragment extends SherlockFragment {
    private static final String TAG = "UVDetailFragment";

    /**
     * The fragment argument representing the UV ID that this fragment
     * represents.
     */
    public static final String ARG_UV_ID = "item_id";

    /**
     * The UV this fragment is presenting.
     */
    private UVwebContent.UV mUV;

    /**
     * The ListView containing all comment items.
     */
    private ListView mListView;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public UVDetailFragment() {
    }

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

        // Fragment configuration
        setHasOptionsMenu(true);
        setRetainInstance(true);

        if (getArguments().containsKey(ARG_UV_ID)) {
            // Load the UV specified by the fragment
            // arguments. In a real-world scenario, use a Loader
            // to load content from a content provider.
            mUV = UVwebContent.UV_MAP.get(getArguments().getString(
                    ARG_UV_ID));

            getSherlockActivity().getSupportActionBar().setTitle(mUV.toString());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_uv_detail,
                container, false);

        mListView = (ListView) rootView.findViewById(android.R.id.list);

        UVCommentAdapter adapter = new UVCommentAdapter(getSherlockActivity());
        adapter.updateComments(UVwebContent.COMMENTS);

        // Show the UV as text in a TextView.
        if (mUV != null) {
            View headerView = rootView.findViewById(android.R.id.empty);
            if (adapter.isEmpty()) {
				ViewStub headerViewStub = (ViewStub) headerView;
				headerViewStub.setOnInflateListener(new ViewStub.OnInflateListener() {
                    @Override
                    public void onInflate(ViewStub stub, View inflated) {
                	mListView.setEmptyView(inflated);
                    setHeaderData(inflated);
                    }
                });
				headerViewStub.inflate();
				headerViewStub = null;
            } else {
                headerView = inflater.inflate(R.layout.uv_detail_header, null);
                setHeaderData(headerView);
                mListView.addHeaderView(headerView);
            }
        }

        mListView.setAdapter(adapter);

        return rootView;
    }

    private void setHeaderData(View inflatedHeader) {
        ((TextView) inflatedHeader.findViewById(R.id.uvcode)).setText(Html.fromHtml(String.format(
				UVwebContent.UV_TITLE_FORMAT, mUV.getLetterCode(), mUV.getNumberCode())));
        ((TextView) inflatedHeader.findViewById(R.id.desc)).setText(mUV.getDescription());
        ((TextView) inflatedHeader.findViewById(R.id.rate)).setText(mUV.getFormattedRate());
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.fragment_uv_detail, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_refresh:
                Toast.makeText(getActivity(), "Refresh clicked", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

@@ -22,16 +22,58 @@
 uvweb;%0A%0A
+import android.content.res.Configuration;%0A
 import a
@@ -1716,24 +1716,328 @@
 (true);%0A
-        
+%0A%09%09final Configuration config = getResources().getConfiguration();%0A%09%09if (config.smallestScreenWidthDp != 600 %7C%7C config.orientation != Configuration.ORIENTATION_LANDSCAPE) %7B%0A%09%09%09// Workaround, we do not want to retain instance for a Nexus 7 for instance,%0A%09%09%09// since in portrait mode only the list is displayed%0A%09%09%09
 setRetai
@@ -2052,16 +2052,20 @@
 e(true);
+%0A%09%09%7D
 %0A%0A      

Fix N7 rotation crash with a workaround