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 PromoteAppAdapter extends BufferViewPagerAdapter {
    class Item implements OnClickListener {
        public String name;
        public String down;
        public String imgUrl;
        public Bitmap img;

        @Override
        public void onClick(View v) {
            try {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(down);
                intent.setData(content_url);
                mContext.startActivity(intent);
            } catch (Exception e) {
            }
        }
    }

    class ViewHolder {
        public ImageView back;
    }

    private final ArrayList<Item> item = new ArrayList<Item>();
    private final LayoutInflater inflater;
    private final PromoteAppAdapterHandler handler = new PromoteAppAdapterHandler(
            this);

    public PromoteAppAdapter(Context context, Banner pager) {
        super(context, pager);
        inflater = LayoutInflater.from(context);
        new Thread() {
            @Override
            public void run() {
                try {
                    String array = new String(
                            HttpUtil.getUrlContent("http://setqqv8.top:8080/SQv8/appinfo"),
                            "UTF-8");
                    Log.v("TAG", array);
                    JSONArray arr = new JSONArray(array);
                    for (int i = 0; i < arr.length(); i++) {
                        JSONObject obj = arr.optJSONObject(i);
                        Item ie = new Item();
                        if (obj.optBoolean("show", true)) {
                            ie.name = obj.optString("name");
                            ie.down = obj.optString("down", "");
                            ie.imgUrl = obj.optString("img", "");
                            item.add(ie);
                        }
                    }
                    if (item.size() > 0) {
                        bufferImage(0);
                    }
                } catch (JSONException | UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        handler.startAutoScroll();
    }

    private void bufferImage(final int index) {

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

    @Override
    public Banner getBanner() {
        return this.pager;
    }

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

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

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

    public void safeAdd(int index, Bitmap decodeByteArray) {

        if (decodeByteArray == null) {
            return;
        }
        this.item.get(index).img = decodeByteArray;
        handler.sendEmptyMessage(0);
    }
}
