package de.sebse.fuplanner.tools.ui.treeview;

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

import de.sebse.fuplanner.R;
import de.sebse.fuplanner.services.kvv.types.Resource;

/**
 * Created by tlh on 2016/10/1 :)
 */

public class DirectoryNodeBinder extends TreeViewBinder<DirectoryNodeBinder.ViewHolder> {
    @Override
    public ViewHolder provideViewHolder(View itemView) {
        return new ViewHolder(itemView);
    }

    @Override
    public void bindView(ViewHolder holder, int position, TreeNode node) {
        holder.ivArrow.setRotation(0);
        holder.ivArrow.setImageResource(R.drawable.ic_keyboard_arrow_right);
        int rotateDegree = node.isExpand() ? 90 : 0;
        holder.ivArrow.setRotation(rotateDegree);
        Resource.Folder dirNode = (Resource.Folder) node.getContent();
        holder.tvName.setText(dirNode.getTitle());
        if (node.isLeaf())
            holder.ivArrow.setVisibility(View.INVISIBLE);
        else holder.ivArrow.setVisibility(View.VISIBLE);
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_dir;
    }

    public static class ViewHolder extends TreeViewBinder.ViewHolder {
        private final ImageView ivArrow;
        private final TextView tvName;

        ViewHolder(View rootView) {
            super(rootView);
            this.ivArrow = rootView.findViewById(R.id.iv_arrow);
            this.tvName = rootView.findViewById(R.id.tv_name);
        }

        public ImageView getIvArrow() {
            return ivArrow;
        }

        public TextView getTvName() {
            return tvName;
        }
    }
}
