/*
 * 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.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import com.antew.redditinpictures.library.imgur.ImgurImageApi.Image;
import com.antew.redditinpictures.library.imgur.ImgurImageApi.ImgurImage;
import com.antew.redditinpictures.library.util.ImageUtil;
import com.antew.redditinpictures.library.util.Ln;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

/**
 * 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;

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

    /**
     * Factory method to generate a new instance of the fragment given an {@link ImgurImage}
     *
     * @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;
    }

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

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

    @Override
    public void populatePostData(View v) {
        // Normally has the details of the reddit post (e.g.
        // "Android * 120 Comments * monkeyonatypewriter")
        mPostInformation.setTextSize(14);

        // Hide the number of votes
        mPostVotes.setVisibility(View.GONE);

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

        if (hasTitle || hasCaption) {
            if (hasTitle) {
                Ln.i("Title - %s", image.getTitle());
                mPostTitle.setText(Html.fromHtml(image.getTitle()));
            } else {
                mPostTitle.setVisibility(View.GONE);
            }

            if (hasCaption) {
                Ln.i("Caption - %s", image.getCaption());
                mPostInformation.setText(Html.fromHtml(image.getCaption()));
            } else {
                mPostInformation.setVisibility(View.GONE);
            }
        } else {
            v.setVisibility(View.GONE);
            v.invalidate();
        }
    }

    @Override
    protected void resolveImage() {
        String imageUrl = mImage.getLinks().getOriginal();
        if (ImageUtil.isGif(imageUrl)) {
            super.loadGifInWebView(imageUrl);
        } else {
            Picasso.with(getActivity())
                   .load(Uri.parse(imageUrl))
                   .resize(mScreenSize.getWidth(), mScreenSize.getHeight())
                   .centerInside()
                   .into(mImageView, new Callback() {
                       @Override
                       public void onSuccess() {
                           if (mProgress != null) {
                               mProgress.setVisibility(View.GONE);
                           }
                       }

                       @Override
                       public void onError() {
                           if (mProgress != null) {
                               mProgress.setVisibility(View.GONE);
                           }

                           if (mErrorMessage != null) {
                               mErrorMessage.setVisibility(View.VISIBLE);
                           }
                           if (mRetry != null) {
                               mRetry.setVisibility(View.VISIBLE);
                           }
                       }
                   });
        }
    }

    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("");
    }
}

@@ -3856,128 +3856,47 @@
     
-if (mProgress != null) %7B%0A                               mProgress.setVisibility(View.GONE);%0A                           %7D
+changeVisibility(mProgress, View.GONE);
 %0A   
@@ -4029,185 +4029,121 @@
     
-if (mProgress != null) %7B%0A                               mProgress.setVisibility(View.GONE);%0A                           %7D%0A%0A                           if (mErrorMessage != null) %7B
+changeVisibility(mProgress, View.GONE);%0A                           changeVisibility(mErrorMessage, View.VISIBLE);
 %0A   
@@ -4170,29 +4170,14 @@
     
-    mErrorMessage.set
+change
 Visi
@@ -4175,32 +4175,40 @@
 hangeVisibility(
+mRetry, 
 View.VISIBLE);%0A 
@@ -4198,36 +4198,32 @@
  View.VISIBLE);%0A
-    
                 
@@ -4254,37 +4254,11 @@
     
-        if (mRetry != null) %7B
+%7D);
 %0A   
@@ -4266,141 +4266,140 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-               mRetry.setVisibility(View.VISIBLE);%0A                           %7D%0A                       %7D%0A                   %7D
+private void changeVisibility(View v, int visibility) %7B%0A        if (v != null) %7B%0A            v.setVisibility(visibility
 );%0A 
@@ -4405,33 +4405,32 @@
         %7D%0A    %7D%0A
-%0A
     private bool

Refactor how visibility is set.