/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.antew.redditinpictures.library.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.antew.redditinpictures.library.R;
import com.antew.redditinpictures.library.imgur.ImgurImageApi.Image;
import com.antew.redditinpictures.library.imgur.ImgurImageApi.ImgurImage;
import com.antew.redditinpictures.library.logging.Log;
import com.antew.redditinpictures.library.utils.Consts;
import com.antew.redditinpictures.library.utils.ImageUtil;

/**
 * This fragment will populate the children of the ViewPager from {@link ImgurAlbumActivity}.
 */
public class ImgurAlbumFragment extends ImageViewerFragment {
    public static final String TAG = "ImgurAlbumFragment";
    private ImgurImage         mImage;

    /**
     * Factory method to generate a new instance of the fragment given an {@link ImgurImage}
     * 
     * @param postData
     *            The post to load
     * @return A new instance of ImageDetailFragment with imageNum extras
     */
    public static ImgurAlbumFragment newInstance(ImgurImage image) {
        final ImgurAlbumFragment f = new ImgurAlbumFragment();
        final Bundle args = new Bundle();
        args.putParcelable(IMAGE_DATA_EXTRA, image);
        f.setArguments(args);

        return f;
    }

    /**
     * Empty constructor as per the Fragment documentation
     */
    public ImgurAlbumFragment() {}

    @Override
    public void loadExtras() {
        mImage = getArguments() != null ? (ImgurImage) getArguments().getParcelable(IMAGE_DATA_EXTRA) : null;
    }

    @Override
    protected void resolveImage() {
        String imageUrl = mImage.getLinks().getOriginal();
        if (ImageUtil.isGif(imageUrl)) {
            mWebView = (WebView) mViewStub.inflate();
            mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
            mWebView.getSettings().setLoadWithOverviewMode(true);
            mWebView.getSettings().setUseWideViewPort(true);
            mWebView.setVisibility(View.VISIBLE);
            mImageView.setVisibility(View.GONE);
            mWebView.setOnTouchListener(getWebViewOnTouchListener());
            new AQuery(getActivity()).id(mWebView).progress(mProgress).webImage(imageUrl);
        } else {
            mImageFetcher.loadImage(imageUrl, mImageView, mProgress, mErrorMessage);
        }
    }

    @Override
    public void populatePostData(View v) {


        // Normally has the title of the Reddit post
        TextView postCaption = (TextView) v.findViewById(R.id.post_title);

        // Normally has the details of the reddit post (e.g.
        // "Android • 120 Comments • monkeyonatypewriter")
        TextView postInformation = (TextView) v.findViewById(R.id.post_information);
        postInformation.setTextSize(14);

        // Wrapper for the votes section of the post details
        RelativeLayout votesWrapper = (RelativeLayout) v.findViewById(R.id.votes_wrapper);
        votesWrapper.setVisibility(View.GONE);

        Image image = mImage.getImage();
        boolean hasTitle = hasTitle();
        boolean hasCaption = hasCaption();

        if (hasTitle || hasCaption) {
            if (hasTitle) {
                Log.i(TAG, "Title - " + image.getTitle());
                postCaption.setText(Html.fromHtml(image.getTitle()));
            } else {
                postCaption.setVisibility(View.GONE);
            }

            if (hasCaption) {
                Log.i(TAG, "Caption - " + image.getCaption());
                postInformation.setText(Html.fromHtml(image.getCaption()));
            } else {
                postInformation.setVisibility(View.GONE);
            }
        } else {
            v.setVisibility(View.GONE);
            v.invalidate();
        }
    }

    @Override
    protected void downloadImage(Intent intent) {
        //@formatter:off
        if (intent.hasExtra(Consts.EXTRA_IMAGE_HASH) && 
                intent.hasExtra(Consts.EXTRA_FILENAME) &&
                mImage != null &&
                mImage.getImage().getHash().equals(intent.getStringExtra(Consts.EXTRA_IMAGE_HASH))) {
            
                new DownloadImageTask().execute(mImage.getLinks().getOriginal(), intent.getStringExtra(Consts.EXTRA_FILENAME));
        }
        //@formatter:on
    }
    
    private boolean hasTitle() {
        return mImage.getImage().getTitle() != null && !mImage.getImage().getTitle().trim().equals("");
    }
    
    private boolean hasCaption() {
        return mImage.getImage().getCaption() != null && !mImage.getImage().getCaption().trim().equals("");
    }

    @Override
    protected boolean shouldShowPostInformation() {
        return hasTitle() || hasCaption();
    }
}

@@ -856,46 +856,8 @@
 ew;%0A
-import android.widget.RelativeLayout;%0A
 impo
@@ -3398,17 +3398,17 @@
 Android 
-%E2%80%A2
+*
  120 Com
@@ -3417,9 +3417,9 @@
 nts 
-%E2%80%A2
+*
  mon
@@ -3579,111 +3579,67 @@
  // 
-Wrapper for the votes section of the post details%0A        RelativeLayout votesWrapper = (RelativeLayout
+Hide the number of votes%0A        TextView votes = (TextView
 ) v.
@@ -3660,21 +3660,18 @@
 .id.
+post_
 votes
-_wrapper
 );%0A 
@@ -3686,15 +3686,8 @@
 otes
-Wrapper
 .set

Update for hiding votes when viewing Imgur albums with the new layout.