package com.wasu.cs.widget.mediacontrol;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wasu.cs.model.DemandPlayinfo;
import com.wasu.cs.model.DemandProgram;
import com.wasu.cs.widget.videoview.WasuPlayerView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import cn.com.wasu.main.R;

/**
 * 顶部提示条
 */
public class PromptHeader extends LinearLayout implements IMediaControllerChildView<MediaController> {

	private MediaController mediaController;

	protected TextView assetName;
	private TextView assetInfo;
	private TextView timeInfo;
	private ImageView timeIcon;

	private static final int REFRESH_TIME = 0;

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

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

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

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

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

		assetName = (TextView) findViewById(R.id.asset_name);
		assetInfo = (TextView) findViewById(R.id.asset_info);
		timeInfo = (TextView) findViewById(R.id.time_info);
		timeIcon = (ImageView) findViewById(R.id.time_icon);
	}

	private String getAssetInfo() {

		if (mediaController == null) {
			return "";
		}

		DemandProgram assetInfo = mediaController.getAssetInfo();
		if (assetInfo == null) {
			return "";
		}

		long fitRate = getContext().getSharedPreferences(MediaController.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).getLong(MediaController.SHARED_PREFERENCES_KEY_CURRENT_RATE, WasuPlayerView.DEFAULT_PLAY_RATE);

		String tag = "";
		Set<Entry<Long, DemandPlayinfo>> entrySet = assetInfo.getPlayinfoList().entrySet();
		for (Entry<Long, DemandPlayinfo> entry : entrySet) {
			long rate = entry.getKey();
			if (rate == fitRate) {
				tag = entry.getValue().getTag();
			}
		}

		switch (assetInfo.getAssetType()) {
			case DemandProgram.SHOWTYPE_MOVIE:
				return tag;
			case DemandProgram.SHOWTYPE_TV_SERIES:
				return "第" + assetInfo.getCurPlayIndex() + "集 " + tag;
			case DemandProgram.SHOWTYPE_NEITHER_MOVIE_NOR_TV:
			case DemandProgram.SHOWTYPE_UNKNOWN:
				return "";
			default:
				return "";
		}
	}

	private String getTimeInfo() {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
		Date curDate = new Date(System.currentTimeMillis());
		return formatter.format(curDate);
	}

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

    private Timer timer = null;
    private Handler timeHandler = null;

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

        this.timeInfo.setText(getTimeInfo());

        DemandProgram assetInfo = mediaController.getAssetInfo();

        if (assetInfo != null) {
            this.assetName.setText(assetInfo.getTitle());
            this.assetInfo.setText(getAssetInfo());
        }

        timeHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                if (msg.what == REFRESH_TIME) {
                    timeInfo.setText(getTimeInfo());
                }
            }
        };

        //刷新时间
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (timeHandler != null) {
                    timeHandler.sendEmptyMessage(REFRESH_TIME);
                }
            }
        }, 60 * 1000, 60 * 1000);
    }

    @Override
    public void onHide(MediaController mediaController) {
        // TODO Auto-generated method stub
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timeHandler != null) {
            timeHandler.removeMessages(REFRESH_TIME);
            timeHandler.removeCallbacksAndMessages(null);
            timeHandler = null;
        }
    }

    @Override
    public boolean onShield(KeyEvent event) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timeHandler != null) {
            timeHandler.removeMessages(REFRESH_TIME);
            timeHandler.removeCallbacksAndMessages(null);
            timeHandler = null;
        }
    }

	@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) {
		// TODO Auto-generated method stub

	}

	@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

	}

	@Override
	public void setRelativeViews(IMediaControllerChildView<MediaController>... views) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<IMediaControllerChildView<MediaController>> getRelativeViews() {
		// TODO Auto-generated method stub
		return null;
	}

}
