
package org.zarroboogs.weibo.adapter;

import org.zarroboogs.weibo.R;
import org.zarroboogs.weibo.bean.UserBean;
import org.zarroboogs.weibo.setting.SettingUtils;
import org.zarroboogs.weibo.support.asyncdrawable.TimeLineBitmapDownloader;
import org.zarroboogs.weibo.support.utils.ThemeUtility;
import org.zarroboogs.weibo.support.utils.Utility;
import org.zarroboogs.weibo.support.utils.ViewUtility;
import org.zarroboogs.weibo.widget.TimeLineAvatarImageView;

import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

public class UserListAdapter extends BaseAdapter {

    protected List<UserBean> bean;

    protected Fragment activity;

    protected LayoutInflater inflater;

    protected ListView listView;

    protected TimeLineBitmapDownloader commander;

    protected int checkedBG;

    protected int defaultBG;

    public UserListAdapter(Fragment activity, List<UserBean> bean, ListView listView) {
        this.bean = bean;
        this.commander = TimeLineBitmapDownloader.getInstance();
        this.inflater = activity.getActivity().getLayoutInflater();
        this.listView = listView;
        this.activity = activity;

        defaultBG = activity.getResources().getColor(R.color.transparent);
        checkedBG = ThemeUtility.getColor(R.attr.listview_checked_color);

        listView.setRecyclerListener(new AbsListView.RecyclerListener() {
            @Override
            public void onMovedToScrapHeap(View view) {
                ViewHolder holder = (ViewHolder) view.getTag();
                if (holder == null) {
                    return;
                }
                holder.avatar.setImageDrawable(null);
            }
        });
    }

    private List<UserBean> getList() {
        return bean;
    }

    @Override
    public int getCount() {

        if (getList() != null && getList().size() > 0) {
            return getList().size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return getList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.user_listview_item_layout, parent, false);
            holder.username = ViewUtility.findViewById(convertView, R.id.username);
            holder.content = ViewUtility.findViewById(convertView, R.id.content);
            holder.avatar = (TimeLineAvatarImageView) convertView.findViewById(R.id.avatar);
            holder.listview_root = ViewUtility.findViewById(convertView, R.id.listview_root);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        configViewFont(holder);
        configLayerType(holder);
        bindViewData(holder, position);

        return convertView;
    }

    private void bindViewData(ViewHolder holder, int position) {

        holder.listview_root.setBackgroundColor(defaultBG);

        if (listView.getCheckedItemPosition() == position + 1) {
            holder.listview_root.setBackgroundColor(checkedBG);
        }

        UserBean user = getList().get(position);
        holder.avatar.checkVerified(user);
        holder.avatar.setPressesStateVisibility(false);
        holder.username.setText(user.getScreen_name());
        String image_url = user.getAvatar_large();
        if (!TextUtils.isEmpty(image_url)) {
            commander.downloadAvatar(holder.avatar.getImageView(), user, false);
        }
        holder.content.setText(user.getDescription());

    }

    private void configLayerType(ViewHolder holder) {

        boolean disableHardAccelerated = SettingUtils.disableHardwareAccelerated();
        if (!disableHardAccelerated) {
            return;
        }

        int currentWidgetLayerType = holder.username.getLayerType();

        if (View.LAYER_TYPE_SOFTWARE != currentWidgetLayerType) {
            holder.username.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            if (holder.content != null) {
                holder.content.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            }

        }

    }

    private void configViewFont(ViewHolder holder) {
        int prefFontSizeSp = SettingUtils.getFontSize();
        float currentWidgetTextSizePx;

        currentWidgetTextSizePx = holder.content.getTextSize();

        if (Utility.sp2px(prefFontSizeSp) != currentWidgetTextSizePx) {
            holder.content.setTextSize(prefFontSizeSp);
            holder.username.setTextSize(prefFontSizeSp);
        }
    }

    private class ViewHolder {

        TextView username;

        TextView content;

        TimeLineAvatarImageView avatar;

        RelativeLayout listview_root;
    }

    public void removeItem(UserBean item) {
        getList().remove(item);
        notifyDataSetChanged();
    }

    public void update(UserBean oldValue, UserBean newValue) {
        int index = getList().indexOf(oldValue);
        getList().remove(index);
        getList().add(index, newValue);
        notifyDataSetChanged();
    }
}
