package com.wasu.cs.widget.videoview;

import android.content.Context;
import android.text.TextUtils;

import com.wasu.authsdk.AuthListener;
import com.wasu.authsdk.AuthSDK;
import com.wasu.comp.pay.DialogPay;
import com.wasu.comp.userlogin.DialogLogin;
import com.wasu.comp.videoview.PayInfo;
import com.wasu.compfactory.WasuCompFactory;
import com.wasu.cs.widget.mediacontrol.DialogPlanBuy;
import com.wasu.module.log.WLog;
import com.wasu.statistics.WasuStatistics;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chenliang
 * 2016/12/12
 */

public class WasuPlayerViewPayUtil {
    /**
     * 判断需要支付的类型
     * */
    public static final int PAYTYPE_SINGLE_WASU = 0;//华数单点
    public static final int PAYTYPE_PACKAGE_WASU = 1;//华数包
    public static final int PAYTYPE_PACKAGE_FEIHU = 2; //飞狐包
    public static final int PAYTYPE_SINGLE_MIGU = 3; // 咪咕单点

    private static WasuPlayerViewPayUtil sInstance = null;

    private WasuPlayerViewPayUtil(){}

    public static WasuPlayerViewPayUtil getInstance(){
        if(sInstance == null){
            synchronized(WasuPlayerViewPayUtil.class){
                if(sInstance == null){
                    sInstance = new WasuPlayerViewPayUtil();
                }
            }
        }

        return sInstance;
    }

    /**
     * 检测用户是否已经登陆
     */
    public boolean checkLogin() {
        String userKey = AuthSDK.getInstance().getValue(AuthSDK.KEY_USERKEY);
        if (TextUtils.isEmpty(userKey)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 登陆
     */
    public void ShowLoginUI(Context context, String loginurl, DialogLogin.LoginStatusListener loginStatusListener) {
        DialogLogin mDialogLogin = new WasuCompFactory().createUniLogin(context, loginurl);
        mDialogLogin.setLoginStatusListener(loginStatusListener);
        mDialogLogin.show();
    }

    /**
     * 判断需要订包/单点类型
     */
    public int checkPayType(double price, int AssetFrom) {
        int mPayType = PAYTYPE_SINGLE_WASU;

        if (price == 99999) { //订包
            if (AssetFrom == 91) {
                mPayType = PAYTYPE_PACKAGE_FEIHU;
            } else if (AssetFrom < 91) {
                mPayType = PAYTYPE_PACKAGE_WASU;
            }
        } else {
            if (AssetFrom == 93){
                mPayType = PAYTYPE_SINGLE_MIGU;
            }else {
                mPayType = PAYTYPE_SINGLE_WASU;
            }
        }
        return mPayType;
    }


    /**
     * 询价
     */
    protected void checkPriceFromServere(String resourceId, String resourceName, AuthListener authListener) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("resourceId", resourceId);
        params.put("resourceName", resourceName);
        params.put("orderType", 0);
        AuthSDK.getInstance().queryPrice(params, authListener);
    }

    /**
     * 飞狐资产订包
     *
     * @param OtherOrderUrl     三方订包地址
     * @param assetFromLabel    资产类型
     * @param payStatusListener 飞狐定包回调
     */
    protected void SelectFeiHuPackge(Context context, String OtherOrderUrl, String assetFromLabel, DialogPlanBuy.PayStatusListener payStatusListener) {
        DialogPlanBuy mDialogFeiHuPlanBuy = new DialogPlanBuy(context, OtherOrderUrl, assetFromLabel, payStatusListener);
        mDialogFeiHuPlanBuy.show();
    }

    /**
     * 华数月包
     *
     * @param name     影片名称
     * @param id       影片id或产品包id
     * @param price    价格
     * @param listener 支付结果状态监听
     */
    public void BookWasuMonthPackage(DialogPay mDialogPay, String name, String id, double price, DialogPay.PayStatusListener listener) {
        if (mDialogPay != null) {
            WLog.i("BigDataStatistics", "调用消费记录");
            WasuStatistics.getInstance().BigDataconsume(id, price + "");

            PayInfo pay = new PayInfo();
            pay.setPayType(PayInfo.PAY_TYPE_PLAN); // 订包
            pay.setResourceName(name);
            pay.setResourceId(id);
            pay.setPrice(price);
            mDialogPay.setPayInfo(pay, listener);

            if (!mDialogPay.isShowing()) {
                mDialogPay.show();
            }
        }
    }

    /**
     * 华数单点
     */
    protected void WasuSinglePay(DialogPay mDialogPay, String name, String id, double price, DialogPay.PayStatusListener listener) {
        if (mDialogPay != null) {
            WLog.i("BigDataStatistics", "调用消费记录");
            WasuStatistics.getInstance().BigDataconsume(id, price + "");

            PayInfo pay = new PayInfo();
            pay.setPayType(PayInfo.PAY_TYPE_DEMAND);
            pay.setResourceName(name);
            pay.setResourceId(id);
            pay.setPrice(price);
            mDialogPay.setPayInfo(pay, listener);

            if (!mDialogPay.isShowing()) {
                mDialogPay.show();
            }
        }
    }

    /**
     * 咪咕单点
     */
    protected void MiGuSinglePay(DialogPay mDialogPay, String name, String id, double price, String OttVodId, DialogPay.PayStatusListener listener) { // 咪咕资产_咪咕影院点单带渠道号migu_yy
        if (mDialogPay != null) {
            WLog.i("BigDataStatistics", "调用消费记录");
            WasuStatistics.getInstance().BigDataconsume(id, price + "");

            PayInfo pay = new PayInfo();
            pay.setPayType(PayInfo.PAY_TYPE_Miguyy);
            pay.setResourceName(name);
            pay.setResourceId(id);
            pay.setPrice(price);
            pay.setThirdIndentID(OttVodId);
            mDialogPay.setPayInfo(pay, listener);

            if (!mDialogPay.isShowing()) {
                mDialogPay.show();
            }
        }
    }
}
