package com.wasu.cs.utils;

import android.text.TextUtils;

import com.wasu.authsdk.AuthSDK;
import com.wasu.authsdk.IAuthInterface;
import com.wasu.cs.model.VipStateModel;
import com.wasu.module.http.HttpRequestModule;
import com.wasu.module.http.RequestParams;
import com.wasu.module.log.WLog;
import com.wasu.widgets.tools.AppUtils;

import basic.BuilderTypeManager.BuildType;
import cn.com.wasu.main.Common;

/**
 * Created by chenliang on 2016/12/15.
 */

public class UserUtils {


    /**
     * 判断是否是VIP用户
     * */
    public static String checkIsVip() {
        String isvip = AuthSDK.getInstance().getValue("vipState");
        return isvip.isEmpty() ? "0" : isvip;
    }

    public static boolean checkIsVipBoolen(){
        String isvip = AuthSDK.getInstance().getValue("vipState");
        if (isvip.isEmpty()){
            return false;
        }else {
            return true;
        }
    }

    /**
     * 更新华数vip信息
     * */
    public static void updateWasuVip(){
        /**请求header**/
        StringBuilder requestSB = new StringBuilder();

        requestSB.append("http://vip.wasu.tv/business/product/productHistory.do?version=");

        requestSB.append("1");
        requestSB.append("&userKey=" + AuthSDK.getInstance().getValue(IAuthInterface.KEY_USERKEY));
        requestSB.append("&token=" + AuthSDK.getInstance().getValue(IAuthInterface.KEY_TOKEN));
        requestSB.append("&siteId=" + BuildType.SITE_ID);
        requestSB.append("&encryptV=" + AuthSDK.getInstance().getValue(IAuthInterface.KEY_ENCRYPTV));

        String publicKey = AppUtils.toURLEncoded(AuthSDK.getInstance().getValue(IAuthInterface.KEY_PUBLICKEY));
        requestSB.append("&publicKey=" + publicKey);

        String vipUrl = requestSB.toString();
        RequestParams requestParams = new RequestParams(vipUrl, null, new RequestParams.RequestListener() {
            @Override
            public boolean onResponse(int i, String s, int i1, Object o) {
                if (!TextUtils.isEmpty(s)) {
                    VipStateModel vipStateModel = JsonUtil.fromJson(s, VipStateModel.class);
                    if (vipStateModel != null) {
                        for (VipStateModel.UpmInfoEntity.PlanAndCategoryBOsEntity data : vipStateModel.getUpmInfo().getPlanAndCategoryBOs()) {
                            if (data != null && Common.WASU_PACKAGE_ID.equalsIgnoreCase(data.getObjectBizId())) {
                                WLog.d("cm", data.getObjectName() + data.getObjectBizId() + data.getExpireTime() + data.getType());
                                //说明订购的是华数VIP包
                                AuthSDK.getInstance().saveValue("vipState", data.getAdFree() + "");
                                AuthSDK.getInstance().saveValue("vipExpireTime", data.getExpireTime() + "");
                            }
                        }
                    }
                } else {
                    WLog.e("danxx", "getUserVipState return String--->null");
                }
                return false;
            }
        });
        HttpRequestModule.getInstance().addTask(requestParams);
    }

    /**
     * 更新华数vip信息
     * */
    public static void updateWasuVip(RequestParams.RequestListener listener){
        /**请求header**/
        StringBuilder requestSB = new StringBuilder();

        requestSB.append("http://vip.wasu.tv/business/product/productHistory.do?version=");

        requestSB.append("1");
        requestSB.append("&userKey=" + AuthSDK.getInstance().getValue(IAuthInterface.KEY_USERKEY));
        requestSB.append("&token=" + AuthSDK.getInstance().getValue(IAuthInterface.KEY_TOKEN));
        requestSB.append("&siteId=" + BuildType.SITE_ID);
        requestSB.append("&encryptV=" + AuthSDK.getInstance().getValue(IAuthInterface.KEY_ENCRYPTV));

        String publicKey = AppUtils.toURLEncoded(AuthSDK.getInstance().getValue(IAuthInterface.KEY_PUBLICKEY));
        requestSB.append("&publicKey=" + publicKey);

        String vipUrl = requestSB.toString();
        RequestParams requestParams = new RequestParams(vipUrl, null, listener);
        HttpRequestModule.getInstance().addTask(requestParams);
    }


}
