package shts.jp.android.nogifeed.views;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.parse.GetCallback;
import com.parse.ParseException;
import com.squareup.otto.Subscribe;

import me.gujun.android.taggroup.TagGroup;
import shts.jp.android.nogifeed.R;
import shts.jp.android.nogifeed.models.Favorite;
import shts.jp.android.nogifeed.models.Member;
import shts.jp.android.nogifeed.models.eventbus.BusHolder;
import shts.jp.android.nogifeed.utils.PicassoHelper;

public class ViewMemberDetailHeader extends LinearLayout {

    private static final String TAG = ViewMemberDetailHeader.class.getSimpleName();

    private View rootView;

    private ImageView profileImageView;
    private ImageView favoriteIcon;

    private TextView nameMainTextView;
    private TextView nameSubTextView;

    private TextView birthdayTextView;
    private TextView bloodTypeTextView;
    private TextView constellationTextView;
    private TextView heightTextView;

    private TagGroup tags;

    public ViewMemberDetailHeader(Context context) {
        this(context, null);
    }

    public ViewMemberDetailHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        rootView = LayoutInflater.from(getContext()).inflate(R.layout.view_member_detail_header, this);
        profileImageView = (ImageView) rootView.findViewById(R.id.profile_image);
        favoriteIcon = (ImageView) rootView.findViewById(R.id.favorite_icon);
        nameMainTextView = (TextView) rootView.findViewById(R.id.name_main);
        nameSubTextView = (TextView) rootView.findViewById(R.id.name_sub);
        birthdayTextView = (TextView) rootView.findViewById(R.id.birthday);
        bloodTypeTextView = (TextView) rootView.findViewById(R.id.blood_type);
        constellationTextView = (TextView) rootView.findViewById(R.id.constellation);
        heightTextView = (TextView) rootView.findViewById(R.id.height);
        tags = (TagGroup) rootView.findViewById(R.id.tags);
    }

    public void setup(final String memberObjectId) {

        if (Favorite.exist(memberObjectId)) {
            favoriteIcon.setVisibility(View.VISIBLE);
        } else {
            favoriteIcon.setVisibility(View.GONE);
        }

        Member.getReference(memberObjectId).fetchIfNeededInBackground(new GetCallback<Member>() {
            @Override
            public void done(Member member, ParseException e) {

                final Context context = getContext();
                PicassoHelper.loadAndCircleTransform(
                        context, profileImageView, member.getProfileImageUrl());

                nameMainTextView.setText(member.getNameMain());
                nameSubTextView.setText(member.getNameSub());

                final Resources res = context.getResources();
                birthdayTextView.setText(res.getString(R.string.property_name_birthday, member.getBirthday()));
                bloodTypeTextView.setText(res.getString(R.string.property_name_blood_type, member.getBloodType()));
                constellationTextView.setText(res.getString(R.string.property_name_constellation, member.getConstellation()));
                heightTextView.setText(res.getString(R.string.property_name_height, member.getHeight()));

                tags.setTags(member.getStatus());
            }
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        BusHolder.get().register(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        BusHolder.get().unregister(this);
        super.onDetachedFromWindow();
    }

    @Subscribe
    public void onChangedFavoriteState(Favorite.ChangedFavoriteState state) {
        if (state.e == null) {
            if (state.action == Favorite.ChangedFavoriteState.Action.ADD) {
                favoriteIcon.setVisibility(View.VISIBLE);
            } else {
                favoriteIcon.setVisibility(View.GONE);
            }
        }
    }

}

@@ -123,25 +123,81 @@
 oid.
-util.AttributeSet
+text.TextUtils;%0Aimport android.util.AttributeSet;%0Aimport android.util.Log
 ;%0Aim
@@ -458,16 +458,40 @@
 cribe;%0A%0A
+import java.util.List;%0A%0A
 import m
@@ -2816,50 +2816,452 @@
     
-PicassoHelper.loadAndCircleTransform(%0A    
+final String objectId = member.getObjectId();%0A%0A                if (%22sdpSHPvZXg%22.equals(objectId) %7C%7C %22ojTXXVhx6n%22.equals(objectId) /* for debug */%0A                        %7C%7C %22uJs1wH6AdB%22.equals(objectId) %7C%7C %22DAkpwgMk8w%22.equals(objectId)) %7B%0A                    profileImageView.setImageResource(R.drawable.kensyusei);%0A%0A                    birthdayTextView.setVisibility(View.GONE);%0A                    bloodTypeTextView.setVisibility(View.GONE);%0A
     
@@ -3279,21 +3279,231 @@
      con
+s
 te
-xt,
+llationTextView.setVisibility(View.GONE);%0A                    heightTextView.setVisibility(View.GONE);%0A                    tags.setVisibility(View.GONE);%0A%0A                %7D else %7B%0A                    final String
  profile
@@ -3511,13 +3511,13 @@
 mage
-View,
+Url =
  mem
@@ -3540,20 +3540,22 @@
 ageUrl()
-)
 ;%0A
-%0A
+    
         
@@ -3566,56 +3566,124 @@
     
-nameMainTextView.setText(member.getNameMain());%0A
+PicassoHelper.loadAndCircleTransform(%0A                            context, profileImageView, profileImageUrl);%0A%0A    
     
@@ -3753,24 +3753,28 @@
             
+    
 final Resour
@@ -3803,24 +3803,28 @@
 esources();%0A
+    
             
@@ -3931,32 +3931,36 @@
                 
+    
 bloodTypeTextVie
@@ -4039,24 +4039,28 @@
 odType()));%0A
+    
             
@@ -4182,32 +4182,36 @@
                 
+ 
+   
 heightTextView.s
@@ -4305,37 +4305,286 @@
     
-tags.setTags(member.getStatus
+    List%3CString%3E statusList = member.getStatus();%0A                    if (statusList != null && !statusList.isEmpty()) %7B%0A                        tags.setTags(member.getStatus());%0A                    %7D%0A                %7D%0A                nameMainTextView.setText(member.getNameMain
 ());

fix NullPointerException