package com.hoolai.access.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.hoolai.access.open.fastaccess.channel.FastSdk;
import com.hoolai.access.open.fastaccess.channel.request.GoodsInfo;
import com.hoolai.access.open.fastaccess.channel.request.PayParams;
import com.hoolai.access.open.fastaccess.utils.ToastUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class ProductUtils {
    public static void showProducts(Activity activity, List<GoodsInfo> list){
        if (list.size() == 0) {
            ToastUtils.toast("商品信息获取为空");
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        ListView listView = new ListView(activity);
        listView.setAdapter(new ArrayAdapter(activity, android.R.layout.simple_list_item_1, android.R.id.text1, list));
        listView.setOnItemClickListener((parent, view1, position, id) -> {
            GoodsInfo payInfo = list.get(position);
            // 游戏支付时需要传的参数 示例
            PayParams payParams = new PayParams();
            payParams.setItemId(payInfo.getItemId());
            payParams.setItemName(payInfo.getItemName());
            payParams.setAmount(Integer.parseInt(payInfo.getItemPrice()));
            payParams.setCount(Integer.valueOf(payInfo.getItemCount()));
            //扩展信息，回调时原样返回,因为各渠道回调参数限制不一致。回调参数暂时只支持长度50。callBackInfo中不要使用这些符号：“|”、“=”、“+”、“/”。
            payParams.setCallbackInfo("支付扩展信息,游戏透传参数"+ UUID.randomUUID().toString().replace("-",""));
            Map<String, String> map = new HashMap<>();
            map.put("isSupportThirdPayment", "true");//是否支持google之外的三方支付
            map.put("payType", "1");// 1 钱包(默认此类型) 2 点卡,短代类支付,需要通过FastSdk.supportShortterm查询
            map.put("currency", payInfo.getShowTag());//币种
            payParams.setOpt(map);
            FastSdk.pay(activity, payParams);
        });
        builder.setView(listView);
        builder.show();
    }
}
