package com.beanpodtech.allinone.utils;

import android.content.Context;

import com.beanpodtech.allinone.bean.VideoBean;
import com.beanpodtech.allinone.widget.LoadingView;

import java.util.HashMap;

public class PlayPreparationUtil {

    private static final String TAG = "PlayPreparationUtil";

    private static PlayPreparationUtil instance = null;
    private Context mContext = null;

    private PlayPreparationUtil(Context context) {
        this.mContext = context;
    }

    public static PlayPreparationUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (TimeCounter.class) {
                if (instance == null) {
                    instance = new PlayPreparationUtil(context);
                }
            }
        }
        return instance;
    }


    private void getPlayAuth(final VideoBean.ListBean bean, final LoadingView loadingView) {
//        OkGoBuilder.getInstance().url(UrlConfig.GET_PLAYAUTH).Builder((Activity) mContext).method(OkGoBuilder.PSOT)
//                .cls(AliVideoBean.class).requestBody(HttpUtils.getRequestJson(getRequestBody(bean)))
//                .callback(new OkGoUtil<AliVideoBean>(AliVideoBean.class) {
//            @Override
//            public void onSuccess(Response<AliVideoBean> response) {
//                loadingView.dismiss();
//                AliVideoBean body = response.body();
//                if (body != null) {
//                    if (body.isAllowPlay()) {
//                        AlivcPlayerActivity.startAlivcPlayerActivity(mContext, body, bean.getCourseId(), bean.getVideoId());
//
//                        //To start prepare work of time counting
//                        TimeCounter timeCounter = TimeCounter.getInstance(mContext);
//                        FileUtil fileUtil = new FileUtil();
//                        byte[] nativeBitmap;
//                        final BuyVideoIdBean[] data = new BuyVideoIdBean[1];
//                        //timeCounter.setBitmapExsitFlag(false);
//                        String fileName = timeCounter.genFileName(SpUtil.getInstace().getString(Contants.USER_NAME, "unknown"),
//                                bean.getVideoId(), System.currentTimeMillis());
//                        if (fileName == null || fileName.length() < 1) {
//                            Log.e(TAG, "getPlayAuth file name is invalid");
//                            return;
//                        }
//                        if (timeCounter.getCurrentFileName() != null && !("".equals(timeCounter.getCurrentFileName()))
//                                && timeCounter.getCurrentFileName().equals(fileName) && timeCounter.isBitmapExsitFlag()) {
//                            Log.d(TAG, "Open the same video");
//                        } else {
//                            timeCounter.reset();//判断是否与上次的看的视频一致,通过filename去判断
//                            timeCounter.setCurrentFileName(fileName);
//                            //get natvie bitmap
//                            if (timeCounter.isBitMapFileExist(fileName)) {
//                                nativeBitmap = fileUtil.readFile(fileName);
//
//                                if (nativeBitmap != null) {
//                                    timeCounter.setVideoSecondBitMap(nativeBitmap);
//                                    timeCounter.setBitmapExsitFlag(true);
//                                    timeCounter.setNativeBitmapExsitFlag(true);
//                                }
//                                //get counter
//                                SpUtil.getInstace().getInt(fileName, 0);
//                            }
//                            timeCounter.setNativeBitmapCheckState(1);
//                        }
//                        //get remote bitmap for test
//                    } else {
//                        ToastUtils.show(mContext, "该视频您无权限观看");
//                    }
//                } else {
//                    ToastUtils.show(mContext, "网络异常");
//                }
//            }
//
//            @Override
//            public void onError(Response<AliVideoBean> response) {
//                loadingView.dismiss();
//                super.onError(response);
//            }
//        }).build();
    }
    private HashMap<String, String> getRequestBody(VideoBean.ListBean bean) {
        HashMap<String, String> map = new HashMap<>();
        map.put("userId", SpUtil.getInstace().getString(Contants.USER_ID, null));
        map.put("videoId", bean.getVideoId());
        map.put("type", "1");
        return map;
    }
}

