package com.hc.webapp.video;

import android.arch.lifecycle.Observer;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.hc.lib.video.IVideoPlayer;
import com.hc.lib.video.OnPlayerPausedListener;
import com.hc.lib.video.VideoPauseDialog;
import com.hc.viewmodel.BaseViewModel;
import com.hc.viewmodel.SingleLiveEvent;
import com.hc.viewmodel.UserViewModel;
import com.hc.webapp.BackEventListener;
import com.hc.webapp.MainActivity;

public class VideoControlDelegate implements VideoPauseDialog.OnPlayItemSelectedListener, OnPlayerPausedListener {

    private FragmentActivity activity;
    private IVideoPlayer videoPlayer;
    private VideoPauseDialog pauseDialog;

    private UserViewModel userViewModel;
    private boolean hadCollectedVideo = false;

    private String uid; // 用户
    private String sourceId;  // 源ID

    private SingleLiveEvent<String> eventSourceId;
    private SingleLiveEvent<Boolean> exitPlayingEvent;
    private SingleLiveEvent<Boolean> playNextEvent;
    private BackEventListener backEventListener;
    private MainActivity mainActivity;

    private boolean isBackPause = false;
    String TAG = "video_control";

    public VideoControlDelegate(FragmentActivity activity, IVideoPlayer videoPlayer) {
        this.videoPlayer = videoPlayer;
        this.activity = activity;
        mainActivity = (MainActivity)activity;
        eventSourceId = new SingleLiveEvent<>();
        exitPlayingEvent = new SingleLiveEvent<>();
        playNextEvent = new SingleLiveEvent<>();
        userViewModel = BaseViewModel.obtainViewModel(activity, UserViewModel.class);
    }

    public void setBackEventListener(BackEventListener backEventListener){
        this.backEventListener = backEventListener;
    }

    public void setFullVideo(boolean fullVideo) {
        videoPlayer.setFullVideo(fullVideo);
        videoPlayer.setLoop(!fullVideo);
    }

    public void setLimitTime(long limitTime) {
        videoPlayer.setLimitTime(limitTime);
    }

    public SingleLiveEvent<Boolean> getExitPlayingEvent() {
        return exitPlayingEvent;
    }

    public SingleLiveEvent<Boolean> getPlayNextEvent() {
        return playNextEvent;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public void setSourceId(String sourceId) {
        this.sourceId = sourceId;
        if (TextUtils.isEmpty(sourceId)) {
            return;
        }
        eventSourceId.setValue(sourceId);
    }

    public void subscribeData() {
        eventSourceId.observe(activity, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                if (TextUtils.isEmpty(uid)) {
                    return;
                }
                userViewModel.hadCollectedSource(uid, sourceId);
            }
        });

        // 观察是否收藏数据源
        userViewModel.hadCollectedSource.observe(activity, new Observer<Boolean>() {

            @Override
            public void onChanged(@Nullable Boolean result) {
                hadCollectedVideo = result == null ? false : result;
                Log.d(TAG,"hadCollectedSource is onChanged,hadCollectedVideo is" + (hadCollectedVideo?"true":"false"));
            }
        });
        // 观察是否收藏/取消收藏成功
        userViewModel.isCollectedSuccess.observe(activity, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean result) {
                hadCollectedVideo = result == null ? false : result;
                if (pauseDialog != null) {
                    Log.d(TAG,"pauseDialog is not null");
                    pauseDialog.setIsCollected(hadCollectedVideo);
                }else{
                    Log.d(TAG,"pauseDialog is null");
                }
            }
        });

        userViewModel.toastEvent.observe(activity, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
               /* if(s.contains("添加成功")){
                    mainActivity.collectSuccess();
                }else if(s.contains("删除成功")){
                    mainActivity.cancelCollectSuccess();
                }*/
                //Toast.makeText(activity, s, Toast.LENGTH_LONG).show();
            }
        });
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            //playResume(false);
            videoPlayer.keyCodeBack();
        }
        else {
            videoPlayer.onKeyDown(keyCode, event);
        }
        return true;
    }

    protected void onResume() {
        if (videoPlayer != null) {
            videoPlayer.onVideoResume();
        }
    }

    protected void onPause() {
        if (videoPlayer != null) {
            videoPlayer.onVideoPause();
        }
    }

    public void onDestroy() {
        if (videoPlayer != null) {
            videoPlayer.onVideoDestroy();
        }
    }

    private void showPauseDialog(boolean hadCollectedVideo,boolean hasComplete) {
        Log.d(TAG,"showPauseDialog ,hadCollectedVideo is" + (hadCollectedVideo?"true":"false"));
        if (pauseDialog == null) {
            pauseDialog = new VideoPauseDialog(videoPlayer.getContext(),hasComplete);
            pauseDialog.setOnPlayItemSelectedListener(this);
        }

        if (!pauseDialog.isShowing()) {
            pauseDialog.show();
        }
        pauseDialog.setHasComplete(hasComplete);
        pauseDialog.setIsCollected(hadCollectedVideo);
    }

    @Override
    public void playReStart() {
        videoPlayer.restart();
    }

    @Override
    public void playResume(boolean play) {
        videoPlayer.pauseOrPlay(play);
    }

    @Override
    public void playCollect() {
        if (hadCollectedVideo) {
            userViewModel.deleteCollection(uid, sourceId);
        } else {
            userViewModel.collectSource(uid, sourceId);
        }
    }

    @Override
    public void playExit(boolean playComplete) {
        exitPlayingEvent.setValue(true);
    }

    @Override
    public void playFavorite(String sourceId,String title) {
        MainActivity mainActivity =(MainActivity)activity;
        mainActivity.favoritePlay(sourceId,title);
    }

    @Override
    public void videoPaused() {
        showPauseDialog(hadCollectedVideo,false);
    }

    @Override
    public void videoCompleted() {
        showPauseDialog(hadCollectedVideo,true);
    }

    @Override
    public void videoToNext() {
        playNextEvent.setValue(true);
    }

    @Override
    public void videoExit() {
        exitPlayingEvent.setValue(true);
        onDestroy();
    }

}
