package com.saki.ui.adapter;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.saki.sqv9.R;

import com.saki.tools.HttpUtil;
import com.saki.ui.Banner;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class PromoteTroopAdapter extends BufferViewPagerAdapter {
    class Item implements OnClickListener {
        public String key;
        public String name;
        public String url;
        public Bitmap back;

        public boolean joinQQGroup() {
            Intent intent = new Intent();
            intent.setData(Uri
                    .parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26k%3D"
                            + key));
            try {
                mContext.startActivity(intent);
                return true;
            } catch (Exception e) {
                return false;
            }
        }

        @Override
        public void onClick(View v) {
            boolean b = joinQQGroup();
            Log.i("JoinGroup", b + "");
        }
    }

    class ViewHolder {
        public ImageView icon;
    }

    private final LayoutInflater inflater;

    public ArrayList<Item> item = new ArrayList<Item>();

    private final PromoteTroopAdapterHandler handler = new PromoteTroopAdapterHandler(
            this);

    public PromoteTroopAdapter(Context c, Banner pager) {
        super(c, pager);
        inflater = LayoutInflater.from(c);
        new Thread() {
            @Override
            public void run() {
                try {
                    String array = new String(
                            HttpUtil.getUrlContent("http://setqqv8.top:8080/addon_info.json"),
                            "GBK");
                    JSONArray arr = new JSONArray(array);
                    for (int i = 0; i < arr.length(); i++) {
                        JSONObject obj = arr.optJSONObject(i);
                        Item ie = new Item();
                        ie.name = obj.optString("name");
                        ie.key = obj.optString("key");
                        ie.url = obj.optString("img");
                        item.add(ie);
                    }
                    if (item.size() > 0) {
                        bufferImage(0);
                    }
                } catch (JSONException | UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void bufferImage(final int index) {

        if (index >= item.size() || item.get(index).back != null)
            return;
        new Thread() {
            @Override
            public void run() {
                byte[] data = HttpUtil.getUrlContent(item.get(index).url);
                safeAdd(index,
                        BitmapFactory.decodeByteArray(data, 0, data.length));
            }
        }.start();
    }

    @Override
    protected View getBufferView(View v, int position) {
        ViewHolder holder;
        if (v == null) {
            v = inflater.inflate(R.layout.item_banner_troop, null);
            holder = new ViewHolder();
            holder.icon = (ImageView) v.findViewById(R.id.back);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        Item i = item.get(position);
        if (i.back == null) {
            holder.icon.setBackgroundResource(R.drawable.bg_groupinfo_default);
            bufferImage(position);
        } else {
            holder.icon.setImageBitmap(i.back);
        }
        v.setOnClickListener(i);
        return v;
    }

    @Override
    public int getCount() {
        return item.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    private void safeAdd(int index, Bitmap map) {
        if (map == null) {
            return;
        }
        this.item.get(index).back = map;
        handler.sendEmptyMessage(0);
    }

}
