package xyz.gsora.toot;

import MastodonTypes.Status;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;

/**
 * Created by gsora on 4/9/17.
 * <p>
 * Custom adapter, useful for listing some statuses.
 */
public class StatusesListAdapter extends RecyclerView.Adapter<StatusesListAdapter.ViewHolder> {

    private Status[] statuses;
    private Context parentCtx;

    public StatusesListAdapter(Status[] s, Context parentCtx) {
        statuses = s;
        this.parentCtx = parentCtx;
    }

    @Override
    public StatusesListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.status_row_layout, parent, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(StatusesListAdapter.ViewHolder holder, int position) {
        Status s = statuses[position];
        Status sb = (Status) s.getReblog();

        if (sb != null) { // this is a boost
            setStatusViewTo(sb.getAccount().getDisplayName(), sb.getContent(), sb.getAccount().getAvatar(), s.getAccount().getDisplayName(), holder);
        } else {
            setStatusViewTo(s.getAccount().getDisplayName(), s.getContent(), s.getAccount().getAvatar(), null, holder);
        }

    }

    @Override
    public int getItemCount() {
        return statuses.length;
    }

    private void setStatusViewTo(String author, String content, String avatar, String booster, StatusesListAdapter.ViewHolder holder) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            holder.statusAuthor.setText(Html.fromHtml(author, Html.FROM_HTML_MODE_COMPACT));
            holder.status.setText(Html.fromHtml(content, Html.FROM_HTML_MODE_COMPACT));
        } else {
            holder.statusAuthor.setText(Html.fromHtml(author));
            holder.status.setText(Html.fromHtml(content));
        }

        Picasso.with(parentCtx).load(avatar).into(holder.avatar);

        if (booster != null) {
            holder.boostAuthor.setVisibility(View.VISIBLE);
            holder.boostAuthor.setText("Boosted by " + booster);
        }
    }

    /*
    Private methods
     */

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView statusAuthor;
        public TextView status;
        public ImageView avatar;
        public TextView boostAuthor;

        public ViewHolder(View v) {
            super(v);
            statusAuthor = (TextView) v.findViewById(R.id.status_author);
            status = (TextView) v.findViewById(R.id.status_text);
            status.setMovementMethod(LinkMovementMethod.getInstance());
            avatar = (ImageView) v.findViewById(R.id.avatar);
            boostAuthor = (TextView) v.findViewById(R.id.boost_author);
        }
    }
}

@@ -314,25 +314,30 @@
 .widget.
-ImageView
+RelativeLayout
 ;%0Aimport
@@ -398,16 +398,69 @@
 Picasso;
+%0Aimport de.hdodenhof.circleimageview.CircleImageView;
 %0A%0A/**%0A *
@@ -1996,34 +1996,34 @@
 hor.setText(
+Cool
 Html.
-fromH
+h
 tml(author, 
@@ -2083,34 +2083,34 @@
 tus.setText(
+Cool
 Html.
-fromH
+h
 tml(content,
@@ -2142,39 +2142,33 @@
 ACT));%0A        %7D
- else %7B
+%0A
 %0A            hol
@@ -2164,72 +2164,75 @@
     
-    holder.statusAuthor.setText(Html.fromHtml(author)
+Picasso.with(parentCtx).load(avatar).into(holder.avatar
 );%0A
+%0A
         
     
@@ -2231,167 +2231,233 @@
     
-    holder.status.setText(Html.fromHtml(content));%0A  
+if (booster != null) %7B%0A
      
- %7D%0A%0A
        
- Picasso.with(parentCtx).load(avatar).into(holder.avatar);%0A%0A        if (booster != null) %7B
+RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) holder.boostAuthor.getLayoutParams();%0A            p.setMargins(0, 16, 0, 0);%0A            holder.boostAuthor.setLayoutParams(p);%0A
 %0A   
@@ -2590,51 +2590,66 @@
     
-%7D%0A
     
-%7D%0A
+holder.boostAuthor.setTextSize(12.0f);
 %0A    
-/*%0A
     
-Private methods
+%7D
 %0A    
- */
+%7D
 %0A%0A  
@@ -3059,16 +3059,22 @@
  public 
+Circle
 ImageVie
@@ -3413,16 +3413,22 @@
 atar = (
+Circle
 ImageVie
@@ -3546,11 +3546,49 @@
  %7D%0A    %7D
+%0A%0A    /*%0A    Private methods%0A     */%0A%0A
 %0A%7D%0A

use CoolHtml to parse html for each status; new boost mechanism the boost textview is now partially computed here, instead of hardcoding the values in the xml take into account the fact that it's not an imageview anymore, it's a CircleImageView