package com.concavenp.artistrymuse.fragments.viewholder;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.concavenp.artistrymuse.R;
import com.concavenp.artistrymuse.StorageDataType;
import com.concavenp.artistrymuse.UserInteractionType;
import com.concavenp.artistrymuse.interfaces.OnInteractionListener;
import com.concavenp.artistrymuse.model.UserResponseHit;

/**
 * Created by dave on 12/2/2016.
 */
public class UserResponseViewHolder extends BaseViewHolder {

    /**
     * The logging tag string to be associated with log data for this class
     */
    @SuppressWarnings("unused")
    private static final String TAG = UserResponseViewHolder.class.getSimpleName();

    public UserResponseViewHolder(View itemView) {

        super(itemView);

    }

    @Override
    public void bindToPost(Object pojoJson, final OnInteractionListener listener) {

        UserResponseHit response;

        // We are expected an Following object and nothing else
        if (pojoJson instanceof UserResponseHit) {

            response = (UserResponseHit) pojoJson;

        }
        else {

            Log.e(TAG, "Unexpected object type found when expecting an UserResponseHit object");

            return;

        }

        // Display items to be populated
        final ImageView headerImageView = (ImageView) itemView.findViewById(R.id.header_ImageView);
        final ImageView profileImageView = (ImageView) itemView.findViewById(R.id.avatar_ImageView);
        final TextView authorTextView = (TextView) itemView.findViewById(R.id.author_TextView);
        final TextView usernameTextView = (TextView) itemView.findViewById(R.id.username_TextView);
        final TextView descriptionTextView = (TextView) itemView.findViewById(R.id.description_textView);
        final TextView followedTextView = (TextView) itemView.findViewById(R.id.followed_textview);
        final TextView followingTextView = (TextView) itemView.findViewById(R.id.views_textView);

        // Verify there is data to work with
        if (response._source != null) {

            populateImageView(
                    buildFileReference(
                            response.get_source().getUid(),
                            response.get_source().getHeaderImageUid(),
                            StorageDataType.USERS),
                    headerImageView);
            populateImageView(
                    buildFileReference(
                            response.get_source().getUid(),
                            response.get_source().getProfileImageUid(),
                            StorageDataType.USERS),
                    profileImageView);
            populateTextView(
                    "@" + response.get_source().getUsername(),
                    usernameTextView);
            populateTextView(
                    response.get_source().getSummary(),
                    authorTextView);
            populateTextView(
                    response.get_source().getDescription(),
                    descriptionTextView);
            populateTextView(
                    Integer.toString(response.get_source().getFollowedCount()),
                    followedTextView);
            populateTextView(
                    Integer.toString(response.get_source().getFollowing().size()),
                    followingTextView);

            // Create stable UID for override
            final String uid = response.get_source().getUid();

            // Add a click listener to the view in order for the user to get more details about a selected movie
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    // Notify the the listener (aka MainActivity) of the details selection
                    listener.onInteractionSelection(uid, null, StorageDataType.USERS, UserInteractionType.DETAILS);

                }
            });

        }

    }

}


@@ -2736,36 +2736,16 @@
 extView(
-%0A                   
  %22@%22 + r
@@ -2779,36 +2779,16 @@
 rname(),
-%0A                   
  usernam
@@ -2828,36 +2828,16 @@
 extView(
-%0A                   
  respons
@@ -2864,36 +2864,16 @@
 mmary(),
-%0A                   
  authorT
@@ -2911,36 +2911,16 @@
 extView(
-%0A                   
  respons
@@ -2955,80 +2955,198 @@
 n(),
-%0A                    descriptionTextView);%0A            populateTextView(
+ descriptionTextView);%0A%0A            // Testing seems to have introduced situations where data that is to be converted from%0A            // string to integer may be null thus needs to handled.
 %0A   
@@ -3150,32 +3150,63 @@
             
- 
+try %7B%0A
        
+         populateTextView(
 Integer.toSt
@@ -3248,24 +3248,43 @@
 wedCount()),
+ followedTextView);
 %0A           
@@ -3275,32 +3275,35 @@
 ew);%0A           
+ %7D%0A
          followe
@@ -3299,76 +3299,335 @@
     
-followedTextView);%0A            populateTextView(%0A                   
+   catch (NullPointerException ex) %7B%0A                populateTextView(%22?%22, followedTextView);%0A            %7D%0A%0A            // Testing seems to have introduced situations where data that is to be converted from%0A            // string to integer may be null thus needs to handled.%0A            try %7B%0A                populateTextView(
  Int
@@ -3685,16 +3685,36 @@
 size()),
+ followingTextView);
 %0A       
@@ -3707,32 +3707,37 @@
 View);%0A         
+   %7D%0A
            follo
@@ -3730,16 +3730,89 @@
         
+  catch (NullPointerException ex) %7B%0A                populateTextView(%22?%22,
  followi
@@ -3815,32 +3815,46 @@
 lowingTextView);
+%0A            %7D
 %0A%0A            //

Fix bug with searching results where data returned does not have values for numerical objects