package com.saki.ui.adapter;

import java.util.Iterator;

import com.saki.sqv9.R;

import com.saki.qq.global.TroopManager;
import com.saki.qq.wtlogin.pack.jce.Troop;
import com.saki.tools.BitmapCallback;
import com.saki.tools.BitmapUtil;
import com.saki.ui.SwitchButton;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TroopAdapter extends BaseAdapter {
    class ItemHolder {
        public ImageView icon;
        public TextView title;
        public TextView id;
        public TextView count;
        public SwitchButton opration;
    }

    class TroopAdapterHandler extends Handler {
        class TroopMessage {
            public long id;
            public Bitmap icon;

            public TroopMessage(long id, Bitmap icon) {
                this.id = id;
                this.icon = icon;
            }
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    TroopAdapter.this.notifyDataSetChanged();
                    break;
                case 1:
                    TroopAdapter.this.updateTroopCount((long) msg.obj);
                    break;
                case 2:
                    TroopMessage tm = (TroopMessage) msg.obj;
                    TroopAdapter.this.updateTroopIcon(tm.id, tm.icon);
                    break;
                default:
                    break;
            }
        }

        public void notifyDataSetChange() {
            sendEmptyMessage(0);
        }

        public void updateTroopCount(long id) {
            Message m = Message.obtain();
            m.what = 1;
            m.obj = id;
            sendMessage(m);
        }

        public void updateTroopIcon(long id, Bitmap map) {
            Message m = Message.obtain();
            m.what = 2;
            m.obj = new TroopMessage(id, map);
            sendMessage(m);
        }
    }

    private static final int LAYOUT_TROOP = R.layout.item_troop;
    private static final int ID_ICON = R.id.icon;
    private static final int ID_TITLE = R.id.title;
    private static final int ID_NUMBER = R.id.number;
    private static final int ID_OPRATION = R.id.opration;
    private static final int ID_COUNT = R.id.count;
    private final LayoutInflater inflater;

    private final TroopAdapterHandler handler = new TroopAdapterHandler();

    public TroopAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return TroopManager.getInstance().troop.size();
    }

    @Override
    public Troop getItem(int arg0) {
        return TroopManager.getInstance().troop.get(arg0);
    }

    @Override
    public long getItemId(int position) {
        Troop i = getItem(position);
        return i == null ? 0 : i.id;
    }

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

    private Troop getTroop(long id) {
        Iterator<Troop> it = TroopManager.getInstance().troop.iterator();
        while (it.hasNext()) {
            Troop t = it.next();
            if (t.id == id)
                return t;
        }
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ItemHolder holder;
        if (convertView == null) {
            holder = new ItemHolder();
            convertView = inflater.inflate(LAYOUT_TROOP, null);
            holder.icon = (ImageView) convertView.findViewById(ID_ICON);
            holder.count = (TextView) convertView.findViewById(ID_COUNT);
            holder.id = (TextView) convertView.findViewById(ID_NUMBER);
            holder.title = (TextView) convertView.findViewById(ID_TITLE);
            holder.opration = (SwitchButton) convertView
                    .findViewById(ID_OPRATION);
            convertView.setTag(holder);
        } else {
            holder = (ItemHolder) convertView.getTag();
        }
        final Troop item = getItem(position);
        if (item != null) {
            holder.count.setText(String.valueOf(item.count));
            if (item.icon != null) {
                holder.icon.setImageBitmap(item.icon);
            } else {
                holder.icon.setImageResource(R.drawable.face_troop_default);
                if (!item.loadlost) {
                    item.loadlost = true;
                    BitmapUtil.load(item.id + "", getItemViewType(position),
                            new BitmapCallback() {
                                @Override
                                public void callBack(Bitmap bit) {
                                    if (bit != null) {
                                        updateIcon(item.id, bit);
                                    }
                                }
                            });
                }
            }
            holder.id.setText(String.valueOf(item.id));
            holder.title.setText(item.name);
            holder.opration.setChecked(item.opration);
            holder.opration.setOnCheckedChangeListener(item);
        }
        return convertView;
    }

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

    public void notifyChange() {
        handler.notifyDataSetChange();
    }

    public void updataCount(long id) {
        handler.updateTroopCount(id);
    }

    public void updateIcon(long id, Bitmap bit) {
        handler.updateTroopIcon(id, bit);
    }

    private boolean updateTroopCount(long id) {
        Troop item = getTroop(id);
        if (item == null)
            return false;
        item.count += 1;
        notifyDataSetChanged();
        return true;
    }

    private boolean updateTroopIcon(long id, Bitmap icon) {
        Troop item = getTroop(id);
        if (item == null)
            return false;
        item.icon = icon;
        notifyDataSetChanged();
        return true;
    }

}
