package com.wasu.cs.widget.videoview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.widget.FrameLayout;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.wasu.comp.videoview.IMediaControl;
import com.wasu.comp.videoview.IMediaListener;
import com.wasu.cs.model.ConfigData;
import com.wasu.cs.model.ILiveAssets;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;
import com.wasu.module.datafetch.DataFetchListener;
import com.wasu.module.datafetch.DataFetchModule;
import com.wasu.module.log.WLog;

import org.json.JSONObject;

import cn.com.wasu.main.Common;
import cn.com.wasu.main.R;


public class LivePlayerMask extends FrameLayout implements IMediaListener {

    private static final String TAG ="LivePlayerMask" ;
    private ILiveAssets liveInfo;
    private IMediaControl player;

    private boolean isFullScreen;

    public ILiveAssets getAssetInfo() {
        return liveInfo;
    }

    public void setAssetInfo(ILiveAssets liveInfo) {
        this.liveInfo = liveInfo;
    }

    public IMediaControl getPlayer() {
        return player;
    }

    public void setPlayer(IMediaControl player) {
        this.player = player;
    }

    private SimpleDraweeView loadingView;

    public LivePlayerMask(Context context) {
        super(context);

        setFocusable(false);
        setFocusableInTouchMode(false);
        setBackgroundColor(Color.TRANSPARENT);

        init(context);
    }

    private void init(Context context) {
        loadingView = new SimpleDraweeView(context);
        /*loadingView.setBackgroundResource(R.drawable.player_mask_bg);*/
        setLoadingViewBackground();
        loadingView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
        loadingView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        addView(loadingView);
    }

    String url;
    public void setBackgroundUrl(String url) {
        this.url = url;
        FrescoImageFetcherModule.getInstance().attachImage(url, loadingView);
    }

    private void setLoadingViewBackground() {
        if(!TextUtils.isEmpty(url)) {
            FrescoImageFetcherModule.getInstance().attachImage(url, loadingView);
            return;
        }
        loadingView.setImageResource(R.drawable.player_mask_bg);
        SharedPreferences sp = getContext().getSharedPreferences("configData", Context.MODE_PRIVATE);
        String url=sp.getString("playCover","");
        if(url!=null){
            FrescoImageFetcherModule.getInstance().attachImage(url, loadingView);
            return;
        }else{
            loadingView.setImageResource(R.drawable.player_mask_bg);
        }
        DataFetchModule.getInstance().fetchJsonGet(Common.CONFIG_URL, new DataFetchListener.JsonListener() {

            @Override
            public void onJsonGet(int retcode, String extraMsg,JSONObject jsondata) {
                if(retcode != 0){
                    WLog.e(TAG, "fetch config data failure");
                }else{
                    try {
                        Gson gson = new Gson();
                        ConfigData configData = gson.fromJson(jsondata.toString(),ConfigData.class);
                        if(!configData.getData().getPlayCoverPicUrl().isEmpty()){
                            FrescoImageFetcherModule.getInstance().attachImage(configData.getData().getPlayCoverPicUrl(), loadingView);
                        }else{
                            loadingView.setImageResource(R.drawable.player_mask_bg);
                        }
                        saveConfigData(configData);
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                        WLog.e(TAG, "Json data error");
                    }
                }
            }
        });
    }
    /**
     * 把角标信息存储在SharedPreferences中
     * danxingxi
     */
    @SuppressLint("CommitPrefEdits")
    public void saveConfigData(ConfigData configData){
        SharedPreferences  sp = getContext().getSharedPreferences("configData", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        if(configData.getData().getCornerMarks().size()>0){
            for(ConfigData.DataEntity.CornerMarksEntity cornerMark : configData.getData().getCornerMarks()){
                editor.putString(cornerMark.getCmark(), cornerMark.getPicUrl());
            }
        }
        editor.putString("playCover", configData.getData().getPlayCoverPicUrl());
        editor.commit();
    }
    public void handleFullScreen(boolean isFullScreen) {
        this.isFullScreen = isFullScreen;
    }

    @Override
    public void onStart(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onPreparing(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onPrepareComplete(MediaPlayer mediaPlayer) {
        if (loadingView.getParent() != null) {
            removeView(loadingView);
        }
    }

    @Override
    public void onSeeking(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onSeekComplete(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onProgress(int i, int i1, int i2) {
        if (loadingView.getParent() != null) {
            removeView(loadingView);
        }
    }

    @Override
    public void onPause(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onResume(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onStop(MediaPlayer mediaPlayer) {
        if (loadingView.getParent() == null) {
            addView(loadingView);
        }
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onError(MediaPlayer mediaPlayer, int i, int i1) {

    }

    @Override
    public void onInfo(MediaPlayer mediaPlayer, int i, int i1) {

    }

    @Override
    public void onStatusChanged(MediaPlayer mediaPlayer, int i) {

    }

    @Override
    public void onAdStatusChanged(int status, int extra) {
        switch (status) {
            case IMediaListener.AD_START:
                if (loadingView.getParent() != null) {
                    removeView(loadingView);
                }
                break;
            case IMediaListener.AD_END:

                break;
        }
    }

    @Override
    public void onWasuError(int i, String s) {

    }

    @Override
    public void onWasuPlayLimit(int i, String s) {

    }

}
