package com.wasu.cs.widget.mediacontrol;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.wasu.authsdk.AuthSDK;
import com.wasu.comp.ad.AdView;
import com.wasu.comp.ad.WasuAdEngine;
import com.wasu.cs.model.DemandPlayinfo;
import com.wasu.cs.model.DemandProgram;
import com.wasu.cs.widget.videoview.WasuLivePlayerView;
import com.wasu.cs.widget.videoview.WasuPlayerView;
import com.wasu.util.VersionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

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

/**
 * 带息屏功能的暂停提示
 */
public class PromptPauseADOffScreen extends FrameLayout implements IMediaControllerChildView<MediaController> {

    private MediaController mediaController;

    private FrameLayout container;

    private boolean hideFlag;

    private TextView offScreenTv, continueTv;

    private Context mContext;

    public PromptPauseADOffScreen(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public PromptPauseADOffScreen(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PromptPauseADOffScreen(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        setId(MediaController.generateID());

        LayoutInflater.from(context).inflate(R.layout.media_controller_pause_off_screen, this);

        container = (FrameLayout) findViewById(R.id.container);
        offScreenTv = (TextView) findViewById(R.id.off_screen_continue_play);
        continueTv = (TextView) findViewById(R.id.continue_play);
    }

    private String getPlayTag() {
        if (mediaController == null || mediaController.getAssetInfo() == null) {
            return null;
        }

        Set<Entry<Long, DemandPlayinfo>> entrySet = mediaController.getAssetInfo().getPlayinfoList().entrySet();
        long fitRate = getContext().getSharedPreferences(MediaController.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).getLong(MediaController.SHARED_PREFERENCES_KEY_CURRENT_RATE, WasuPlayerView.DEFAULT_PLAY_RATE);
        for (Entry<Long, DemandPlayinfo> entry : entrySet) {
            long rate = entry.getKey();
            String tag = entry.getValue().getTag();

            if (rate == fitRate) {
                return tag;
            }
        }

        return null;
    }

    @Override
    public boolean isAutoHide() {
        return hideFlag;
    }

    @Override
    public void onShow(MediaController mediaController) {
        this.mediaController = mediaController;

        mediaController.getPlayer().pause();

        container.removeAllViews();

        Map<String, String> params = new HashMap<String, String>();

        if (this.mediaController != null && this.mediaController.getAssetInfo() != null) {
            params.put("prol", Common.APP_NAME + "_" + VersionUtils.getVersionName(getContext()));
            params.put("ccid", mediaController.getAssetInfo().getCatId());
            params.put("cid", mediaController.getAssetInfo().getId());
            params.put("pay", mediaController.getAssetInfo().getIsFree() == 1 ? "0" : "1");
            params.put("strrate", getPlayTag());
            params.put("vd", "");
            params.put("ccName", mediaController.getAssetInfo().getCatName());
            params.put("cName", mediaController.getAssetInfo().getTitle());

            params.put("userid", AuthSDK.getInstance().getValue(AuthSDK.KEY_TVID));
            params.put("columnid", mediaController.getAssetInfo().getCatId());
            params.put("programid", mediaController.getAssetInfo().getId());
            params.put("featureid", mediaController.getAssetInfo().getIsFree() == 1 ? "true" : "false");
        }
        AdView view = WasuAdEngine.getInstance().getImageAd(76, R.drawable.ad_wasu_logo, false, params, null);

        container.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        offScreenTv.requestFocus();
    }

    @Override
    public void onHide(MediaController mediaController) {
        hideFlag = false;
        mediaController.getPlayer().start();
    }

    @Override
    public boolean onShield(KeyEvent event) {
        boolean ret = false;
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_DPAD_LEFT:
                    if (continueTv.isFocused()) {
                        offScreenTv.requestFocus();
                    }
                    ret = true;
                    break;
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                    if (offScreenTv.isFocused()) {
                        continueTv.requestFocus();
                    }
                    ret = true;
                    break;
                case KeyEvent.KEYCODE_DPAD_UP:
                case KeyEvent.KEYCODE_DPAD_DOWN:
                case KeyEvent.KEYCODE_VOLUME_UP:
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                case KeyEvent.KEYCODE_VOLUME_MUTE:
                case KeyEvent.KEYCODE_MENU:
                    ret = true;
                    break;
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:

                    if (continueTv.isFocused() && getVisibility() == VISIBLE) {
                        hideFlag = true;
                        if (mediaController.getPlayer() instanceof WasuPlayerView || mediaController.getPlayer() instanceof WasuLivePlayerView) {
                            ((WasuPlayerView) mediaController.getPlayer()).requestFocus();
                        }
                    }
                    if (offScreenTv.isFocused() && getVisibility() == VISIBLE) {
                        hideFlag = true;
                        Intent intent = new Intent();
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setAction("com.wasu.screensaver");
                        mContext.startActivity(intent);
                    }
                    break;
                case KeyEvent.KEYCODE_BACK:
                    hideFlag = true;

                    if (mediaController.getPlayer() instanceof WasuPlayerView || mediaController.getPlayer() instanceof WasuLivePlayerView) {
                        ((WasuPlayerView) mediaController.getPlayer()).requestFocus();
                    }
                    break;
                default:
                    ret = true;
                    break;
            }
        }
        return ret;
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAdStatusChanged(int arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCompletion(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onError(MediaPlayer arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onInfo(MediaPlayer arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPause(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPrepareComplete(MediaPlayer arg0) {
        if (mediaController == null || mediaController.getPlayer() == null) {
            return;
        }

        DemandProgram assetInfo = mediaController.getAssetInfo();

        if (assetInfo == null) {
            return;
        }

        if (assetInfo.getAssetFrom() == 91) {
            mediaController.getPlayer().pause();
        }
    }

    @Override
    public void onPreparing(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProgress(int arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onResume(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSeekComplete(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSeeking(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStart(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(MediaPlayer arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStop(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onWasuError(int arg0, String arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onWasuPlayLimit(int arg0, String arg1) {
        // TODO Auto-generated method stub

    }

    private List<IMediaControllerChildView<MediaController>> relativeViews = new ArrayList<IMediaControllerChildView<MediaController>>();

    @Override
    public void setRelativeViews(IMediaControllerChildView<MediaController>... views) {
        for (IMediaControllerChildView<MediaController> v : views) {
            if (!relativeViews.contains(v)) {
                relativeViews.add(v);
            }
        }
    }

    @Override
    public List<IMediaControllerChildView<MediaController>> getRelativeViews() {
        return relativeViews;
    }

}
