package com.bokecc.livemodule.view.redpacket;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.bokecc.dwlivedemo.R;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 红包雨动画涉及到的资源。
 * @author w
 */

public class RedPacketRes {
    public int defaultWidth;
    public int defaultHeight;
    private Map<Integer, Bitmap> mBitmapMap = new ConcurrentHashMap<>();
    /*普通红包（无序）*/
    public final int[] NORMAL_LIST = {
            R.mipmap.red_oacket0,R.mipmap.red_oacket1,R.mipmap.red_oacket2,R.mipmap.red_oacket3,R.mipmap.red_oacket4,R.mipmap.red_oacket5,R.mipmap.red_oacket6,R.mipmap.red_oacket7,R.mipmap.red_oacket8,R.mipmap.red_oacket9,R.mipmap.red_oacket10
    };

    /*有积分的红包*/
    public final int[] GIFT_LIST = {R.mipmap.red_packet_open1,R.mipmap.red_packet_open2,R.mipmap.red_packet_open3
            ,R.mipmap.red_packet_open4,R.mipmap.red_packet_open5,R.mipmap.red_packet_open6,R.mipmap.red_packet_open7
            ,R.mipmap.red_packet_open8,R.mipmap.red_packet_open9,R.mipmap.red_packet_open10,R.mipmap.red_packet_open11
            ,R.mipmap.red_packet_open12

    };
    /*无积分的红包*/
    public final int[] NO_LIST = {
            //R.mipmap.img_red_packet_no//,R.mipmap.img_red_packet_open
    };
    public static final int TYPE_PACKET = 1; //只有这个类型可以点击
    public static final int TYPE_BOOM = 5; //爆炸
    public static final int TYPE_GIFT = 7; //礼物

    public Bitmap getBitmapFromRes(int imageRes, Resources mResources, BoxInfo redPacket) {
        Bitmap bitmap = null;
        //缓存策略
        if (mBitmapMap.containsKey(imageRes)) {
            bitmap = mBitmapMap.get(imageRes);
        } else {
            if (redPacket.type == TYPE_PACKET){
                bitmap = Bitmap.createScaledBitmap( BitmapFactory.decodeResource(mResources, imageRes),defaultWidth,defaultHeight, true);

            }else if (redPacket.type == TYPE_GIFT){
                bitmap = Bitmap.createScaledBitmap( BitmapFactory.decodeResource(mResources, imageRes),defaultWidth*130/100,defaultHeight*130/100, true);

            }
            if (bitmap!=null)
                mBitmapMap.put(imageRes, bitmap);
        }
        return bitmap;
    }

    public void clear() {
        mBitmapMap.clear();;
    }

    public Bitmap getCurrentBitmao(BoxInfo redPacket, Resources mResources, int rate) {
        int index = redPacket.getmIndex();
        if (redPacket.getType() == TYPE_PACKET){
            int flag = index/rate;
            if (flag>NORMAL_LIST.length-1){
                redPacket.setmIndex(-1);
                flag = 0;
            }
            return getBitmapFromRes(NORMAL_LIST[flag],mResources,redPacket);
        }else if (redPacket.getType() == TYPE_BOOM){
            if (index>NO_LIST.length-1){
                index = NO_LIST.length-1;
            }
            return getBitmapFromRes(NO_LIST[index],mResources,redPacket);
        }else if (redPacket.getType() == TYPE_GIFT){
            int flag = index/rate;
            if (flag>GIFT_LIST.length-1){
                flag = GIFT_LIST.length-1;
            }
            return getBitmapFromRes(GIFT_LIST[flag],mResources,redPacket);
        }
        return null;
    }
}
