/*
 * Copyright 2012 GitHub Inc.
 *
 * 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.github.mobile.ui.ref;

import android.app.Activity;
import android.content.Context;
import android.text.format.Formatter;
import android.view.View;
import android.widget.TextView;

import com.github.kevinsawicki.wishlist.MultiTypeAdapter;
import com.github.mobile.R.id;
import com.github.mobile.R.layout;
import com.github.mobile.core.code.FullTree.Entry;
import com.github.mobile.core.code.FullTree.Folder;
import com.github.mobile.core.commit.CommitUtils;
import com.github.mobile.util.ServiceUtils;
import com.github.mobile.util.TypefaceUtils;

/**
 * Adapter to display a source code tree
 */
public class CodeTreeAdapter extends MultiTypeAdapter {

    private static final int TYPE_BLOB = 0;

    private static final int TYPE_TREE = 1;

    private static final int INDENTED_PADDING = 16;

    private final Context context;

    private final int indentedPaddingLeft ;

    private int paddingLeft ;

    private boolean indented ;

    /**
     * @param activity
     */
    public CodeTreeAdapter(Activity activity) {
        super(activity);

        this.context = activity;
        indentedPaddingLeft = (int) ServiceUtils.getPixels(activity.getResources(), INDENTED_PADDING);
    }

    /**
     * @param context
     */
    public CodeTreeAdapter(Context context) {
        super(context);

        this.context = context;
        indentedPaddingLeft = (int) ServiceUtils.getPixels(context.getResources(), INDENTED_PADDING);
    }

    public void setIndented(boolean indented) {
        this.indented = indented;
    }

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

    /**
     * Set root folder to display
     *
     * @param root
     */
    public void setItems(final Folder root) {
        clear();

        addItems(TYPE_TREE, root.folders.values());
        addItems(TYPE_BLOB, root.files.values());
    }

    @Override
    protected int getChildLayoutId(final int type) {
        switch (type) {
        case TYPE_BLOB:
            return layout.blob_item;
        case TYPE_TREE:
            return layout.folder_item;
        default:
            return -1;
        }
    }

    @Override
    protected int[] getChildViewIds(final int type) {
        switch (type) {
        case TYPE_BLOB:
            return new int[] { id.tv_file, id.tv_size };
        case TYPE_TREE:
            return new int[] { id.tv_folder, id.tv_folders, id.tv_files };
        default:
            return null;
        }
    }

    @Override
    protected View initialize(final int type, View view) {
        view = super.initialize(type, view);
        paddingLeft = view.getPaddingLeft();
        switch (type) {
        case TYPE_BLOB:
            TypefaceUtils.setOcticons((TextView) view
                    .findViewById(id.tv_file_icon));
            break;
        case TYPE_TREE:
            TypefaceUtils.setOcticons(
                    (TextView) view.findViewById(id.tv_folder_icon),
                    (TextView) view.findViewById(id.tv_folders_icon),
                    (TextView) view.findViewById(id.tv_files_icon));
        }

        return view;
    }

    @Override
    protected void update(final int position, final Object item, final int type) {
        if (indented)
            view.setPadding(indentedPaddingLeft, view.getPaddingTop(),view.getPaddingRight(), view.getPaddingBottom());
        else
            view.setPadding(paddingLeft, view.getPaddingTop(),view.getPaddingRight(), view.getPaddingBottom());

        switch (type) {
        case TYPE_BLOB:
            Entry file = (Entry) item;
            setText(0, file.name);
            setText(1, Formatter.formatFileSize(context, file.entry.getSize()));

            break;
        case TYPE_TREE:
            Folder folder = (Folder) item;
            setText(0, CommitUtils.getName(folder.name));
            setNumber(1, folder.folders.size());
            setNumber(2, folder.files.size());
            break;
        }
    }
}


@@ -1463,33 +1463,32 @@
 entedPaddingLeft
- 
 ;%0A%0A    private i
@@ -1501,17 +1501,16 @@
 dingLeft
- 
 ;%0A%0A    p
@@ -1532,17 +1532,16 @@
 indented
- 
 ;%0A%0A    /
@@ -1740,16 +1740,33 @@
 tPixels(
+%0A                
 activity
@@ -2008,16 +2008,33 @@
 tPixels(
+%0A                
 context.
@@ -2075,35 +2075,164 @@
 %0A    %7D%0A%0A    
-public void
+/**%0A     * Set whether views should be indented%0A     *%0A     * @param indented%0A     * @return this adapter%0A     */%0A    public CodeTreeAdapter
  setIndented
@@ -2232,16 +2232,22 @@
 ndented(
+final 
 boolean 
@@ -2292,16 +2292,37 @@
 dented;%0A
+        return this;%0A
     %7D%0A%0A 
@@ -4075,32 +4075,53 @@
 getPaddingTop(),
+%0A                    
 view.getPaddingR
@@ -4229,16 +4229,37 @@
 ngTop(),
+%0A                    
 view.get
@@ -4782,9 +4782,8 @@
     %7D%0A%7D%0A
-%0A

Make setIndented method fluid