package com.datarecovery.master.utils;


import android.text.TextUtils;
import android.util.Log;

import com.atmob.common.data.KVUtils;
import com.datarecovery.master.data.repositories.MemberRepository;
import com.datarecovery.master.handler.EventHelper;
import com.datarecovery.master.module.member.bean.PayInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;

import javax.inject.Singleton;

import atmob.reactivex.rxjava3.annotations.NonNull;
import atmob.reactivex.rxjava3.core.SingleObserver;
import atmob.reactivex.rxjava3.disposables.Disposable;


public class OrderReportHelper {

    private static final String TAG = "OrderReportHelper";

    private static HashMap<String, PayInfo> notReportedPayMap;

    private static final String RECORD_PAY_ORDER_ID_LIST = "record_pay_order_id_list";

    private static Gson gson;
    private static MemberRepository memberRepository;

    private static final long MAX_QUERY_TIME = 60 * 1000L;

    private OrderReportHelper() {

    }

    @Singleton
    public static void init(Gson gson, MemberRepository memberRepository) {
        OrderReportHelper.gson = gson;
        OrderReportHelper.memberRepository = memberRepository;
        initMap();
    }

    private static void initMap() {
        String payInfoString = KVUtils.getDefault().getString(RECORD_PAY_ORDER_ID_LIST, "");
        if (TextUtils.isEmpty(payInfoString)) {
            notReportedPayMap = new HashMap<>();
        } else {
            notReportedPayMap = toGsonMap(payInfoString);
        }
        queryOrderStatus();
    }

    public static void queryOrderStatus() {
        if (notReportedPayMap.size() == 0 || memberRepository == null) {
            return;
        }
        for (String orderId : notReportedPayMap.keySet()) {
            PayInfo payInfo = notReportedPayMap.get(orderId);
            if (payInfo == null) {
                continue;
            }
            if (System.currentTimeMillis() - payInfo.getPayTime() > MAX_QUERY_TIME) {
                removeOderId(orderId);
                continue;
            }
            memberRepository.getPayStatus(orderId).subscribe(new SingleObserver<Boolean>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onSuccess(@NonNull Boolean paySuccess) {
                    Log.d(TAG, "onSuccess: " + paySuccess + "--orderId:" + orderId);
                    if (BoxingUtil.boxing(paySuccess)) {
//                        EventHelper.reportPay(payInfo.getPayAmount(), payInfo.getOrderId(), payInfo.getPayWay(), payInfo.getProduct());
                    }
                }

                @Override
                public void onError(@NonNull Throwable e) {

                }


            });
        }

    }

    private static HashMap<String, PayInfo> toGsonMap(String jsonStr) {
        HashMap<String, PayInfo> res = null;
        try {
            res = gson.fromJson(jsonStr, new TypeToken<HashMap<String, PayInfo>>() {
            }.getType());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (res == null) {
            res = new HashMap<>();
        }
        return res;
    }


    public static void recordOrderId(int payAmount, String orderId, int payWay, String product) {
        if (notReportedPayMap.containsKey(orderId)) {
            return;
        }
        notReportedPayMap.put(orderId, new PayInfo(payAmount, orderId, payWay, product));
        KVUtils.getDefault().putString(RECORD_PAY_ORDER_ID_LIST, gson.toJson(notReportedPayMap));
    }

    public static void removeOderId(String orderId) {
        if (TextUtils.isEmpty(orderId)) {
            return;
        }
        boolean contains = notReportedPayMap.containsKey(orderId);
        if (contains) {
            notReportedPayMap.remove(orderId);
            KVUtils.getDefault().putString(RECORD_PAY_ORDER_ID_LIST, gson.toJson(notReportedPayMap));
        }
    }


}
