/*
 * Copyright (C) 2012 Brian Muramatsu
 *
 * 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.btmura.android.reddit.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.btmura.android.reddit.R;
import com.btmura.android.reddit.entity.Thing;

public class ThingAdapter extends BaseAdapter {

    private final ThumbnailLoader thumbnailLoader = new ThumbnailLoader();
    private final ArrayList<Thing> items = new ArrayList<Thing>();
    private final Context context;
    private final LayoutInflater inflater;
    private final String subredditName;
    private final boolean singleChoice;
    private final long now = System.currentTimeMillis() / 1000;

    private String selectedName;
    private int selectedPosition;
    private int bodyWidth;

    public ThingAdapter(Context context, String subredditName, boolean singleChoice) {
        this.context = context;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.subredditName = subredditName;
        this.singleChoice = singleChoice;
    }

    public void swapData(List<Thing> newItems) {
        items.clear();
        if (newItems != null) {
            items.ensureCapacity(items.size() + newItems.size());
            items.addAll(newItems);
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }
    }

    public void setSelectedThing(String name, int position) {
        selectedName = name;
        selectedPosition = position;
    }

    public String getSelectedThingName() {
        return selectedName;
    }

    public int getSelectedThingPosition() {
        return selectedPosition;
    }

    public boolean isSelectedThing(String name, int position) {
        if (position != selectedPosition) {
            return false;
        }
        if (selectedName != null) {
            return selectedName.equals(name);
        }
        return name == null;
    }

    public void setThingBodyWidth(int bodyWidth) {
        this.bodyWidth = bodyWidth;
    }

    public List<Thing> getItems() {
        return items;
    }

    public int getCount() {
        return items.size();
    }

    public Thing getItem(int position) {
        return items.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).type;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            v = createView(position, parent);
        }
        setView(position, v);
        return v;
    }

    private View createView(int position, ViewGroup parent) {
        switch (getItemViewType(position)) {
            case Thing.TYPE_THING:
                return new ThingView(context);

            case Thing.TYPE_MORE:
                return makeView(R.layout.thing_more_row, parent);

            default:
                throw new IllegalArgumentException();
        }
    }

    private View makeView(int layout, ViewGroup parent) {
        View v = inflater.inflate(layout, parent, false);
        v.setTag(createViewHolder(v));
        return v;
    }

    private static ViewHolder createViewHolder(View v) {
        ViewHolder holder = new ViewHolder();
        holder.title = (TextView) v.findViewById(R.id.title);
        holder.status = (TextView) v.findViewById(R.id.status);
        holder.progress = (ProgressBar) v.findViewById(R.id.progress);
        return holder;
    }

    static class ViewHolder {
        TextView title;
        TextView status;
        ProgressBar progress;
    }

    private void setView(int position, View v) {
        Thing t = getItem(position).assureFormat(context, subredditName, now);
        switch (t.type) {
            case Thing.TYPE_THING:
                setThing((ThingView) v, t, position);
                break;

            case Thing.TYPE_MORE:
                ViewHolder h = (ViewHolder) v.getTag();
                setMore(h, t);
                break;

            default:
                throw new IllegalArgumentException("Unsupported view type: "
                        + getItemViewType(position));
        }
    }

    private void setThing(ThingView v, Thing t, int position) {
        int resId;
        if (singleChoice && position == selectedPosition && t.name.equals(selectedName)) {
            resId = R.drawable.selector_chosen;
        } else {
            resId = R.drawable.selector_normal;
        }
        v.setBackgroundResource(resId);
        v.setBodyWidth(bodyWidth);
        v.setThing(t);
        if (t.hasThumbnail()) {
            thumbnailLoader.setThumbnail(v, t.thumbnail);
        } else {
            thumbnailLoader.clearThumbnail(v);
        }
    }

    private void setMore(ViewHolder h, Thing e) {
        h.title.setCompoundDrawables(null, null, null, null);
    }
}

@@ -856,75 +856,8 @@
 ter;
-%0Aimport android.widget.ProgressBar;%0Aimport android.widget.TextView;
 %0A%0Aim
@@ -1126,16 +1126,18 @@
 %3CThing%3E(
+30
 );%0A    p
@@ -3699,24 +3699,32 @@
  return 
-makeView
+inflater.inflate
 (R.layou
@@ -3747,16 +3747,23 @@
 , parent
+, false
 );%0A%0A    
@@ -3854,634 +3854,8 @@
  %7D%0A%0A
-    private View makeView(int layout, ViewGroup parent) %7B%0A        View v = inflater.inflate(layout, parent, false);%0A        v.setTag(createViewHolder(v));%0A        return v;%0A    %7D%0A%0A    private static ViewHolder createViewHolder(View v) %7B%0A        ViewHolder holder = new ViewHolder();%0A        holder.title = (TextView) v.findViewById(R.id.title);%0A        holder.status = (TextView) v.findViewById(R.id.status);%0A        holder.progress = (ProgressBar) v.findViewById(R.id.progress);%0A        return holder;%0A    %7D%0A%0A    static class ViewHolder %7B%0A        TextView title;%0A        TextView status;%0A        ProgressBar progress;%0A    %7D%0A%0A
     
@@ -4155,95 +4155,8 @@
 RE:%0A
-                ViewHolder h = (ViewHolder) v.getTag();%0A                setMore(h, t);%0A
     
@@ -4889,24 +4889,24 @@
 umbnail(v);%0A
+
         %7D%0A  
@@ -4912,126 +4912,7 @@
    %7D
-%0A%0A    private void setMore(ViewHolder h, Thing e) %7B%0A        h.title.setCompoundDrawables(null, null, null, null);%0A    %7D
 %0A%7D%0A

Remove unnecessary code from ThingAdapter