package com.insidesecure.drm.android.reference.xoplayer.activities.helpers;

import java.util.List;

import android.app.Activity;
import android.view.View;
import android.widget.*;
import com.insidesecure.drm.android.reference.xoplayer.R;
import com.insidesecure.drmagent.DRMContent;
import com.insidesecure.drmagent.mediaplayer.MediaPlayer;
import com.insidesecure.drmagent.subtitles.Subtitle;
import com.insidesecure.drmagent.utils.*;

/** Class encapsulating all dealings with subtitles and audio track selection (UI) as well as display of received subtitles
 * @author
 */
public class AudioSubtitleController implements MediaPlayer.OnTimedTextListener
{

    private final RelativeLayout mCCAudioBox;
    private final DRMContent mDRMContent;
    private final MediaPlayer mMediaPlayer;
    private boolean mCCAudioBoxNeeded;
    private SubtitleHandler mSubtitleHandler;

    public AudioSubtitleController(final Activity activity, final DRMContent drmContent, final MediaPlayer mediaPlayer)
    {
        mCCAudioBox = (RelativeLayout) activity.findViewById(R.id.CC_Audio_Box);
        mDRMContent = drmContent;
        mMediaPlayer = mediaPlayer;

        final Spinner spinnerAudioStreams = (Spinner) activity.findViewById(R.id.Spinner_Audio);
        if (spinnerAudioStreams != null)
        {
            final TextView audioLabel = (TextView) activity.findViewById(R.id.Audio_Label);
            mCCAudioBoxNeeded = setupAudioTracks(activity, drmContent, spinnerAudioStreams, audioLabel) || mCCAudioBoxNeeded;
        }

        Spinner spinnerSubtitleStreams = (Spinner) activity.findViewById(R.id.Spinner_CC);
        if (spinnerSubtitleStreams != null)
        {
            TextView subtitleLabel = (TextView) activity.findViewById(R.id.CC_Label);
            final boolean subtitleHandlingEnabled = setupSubtitleHandling(activity, drmContent, spinnerSubtitleStreams, subtitleLabel);

            if (subtitleHandlingEnabled)
            {
                // The media being played contains subtitle
                mSubtitleHandler = new SubtitleHandler(mediaPlayer,(TextView) activity.findViewById(R.id.subtitle));
                mSubtitleHandler.start();
            }

            mCCAudioBoxNeeded = subtitleHandlingEnabled || mCCAudioBoxNeeded;
        }

        mCCAudioBox.setVisibility(mCCAudioBoxNeeded ? View.VISIBLE : View.GONE);

    }


    private boolean setupSubtitleHandling(final Activity activity, final DRMContent drmContent, final Spinner spinnerSubtitleStreams, final TextView spinnerSubtitleStreamsLabel)
    {
        final List<DRMContent.SubtitleTrack> subtitleTracks = drmContent.getSubtitleTracks();

        if (subtitleTracks.size() > 0)
        {
            //subtitles
            spinnerSubtitleStreamsLabel.setVisibility(View.VISIBLE);
            ArrayAdapter<CharSequence> adapterSpinnerSubtitleStreams = new ArrayAdapter<CharSequence>(activity, android.R.layout.simple_spinner_item);

            spinnerSubtitleStreams.setVisibility(View.VISIBLE);
            spinnerSubtitleStreams.setAdapter(adapterSpinnerSubtitleStreams);

            adapterSpinnerSubtitleStreams.add("None");

            DRMContent.SubtitleTrack selectedSubtitleTrack = drmContent.getSubtitleTrack();
            for (DRMContent.SubtitleTrack subtitleTrack : subtitleTracks)
            {
                adapterSpinnerSubtitleStreams.add(subtitleTrack.mName + " (" + subtitleTrack.mLanguage + ")" + (subtitleTrack.mType == DRMContent.SubtitleTrackType.CLOSED_CAPTIONS ? " - CC" : ""));
                spinnerSubtitleStreams.setSelection(subtitleTracks.indexOf(selectedSubtitleTrack) + 1);
            }

            adapterSpinnerSubtitleStreams.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerSubtitleStreams.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
            {
                public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id)
                {
                    final DRMContent.SubtitleTrack currentSubtitleTrack = drmContent.getSubtitleTrack();

                    if (position == 0)
                    {
                        if (currentSubtitleTrack != DRMContent.NO_SUBTITLE_TRACK)
                        {
                            mMediaPlayer.setSubtitleTrack(DRMContent.NO_SUBTITLE_TRACK);
                            // Clear the subtitle!!
                            // mSubtitleHandler.setSubtitleText(null);
                        }
                    }
                    else
                    {
                        final DRMContent.SubtitleTrack selectedSubtitleTrack = subtitleTracks.get(position - 1);
                        if (!SubtitleTrackHelper.isSame(selectedSubtitleTrack, currentSubtitleTrack))
                        {
                            mMediaPlayer.setSubtitleTrack(selectedSubtitleTrack);

                        }
                    }
                }

                public void onNothingSelected(final AdapterView<?> parent)
                {
                    // Do nothing
                }
            });
            return true;
        }
        else
        {
            spinnerSubtitleStreams.setVisibility(View.GONE);
            spinnerSubtitleStreamsLabel.setVisibility(View.GONE);
            return false;
        }
    }

    private boolean setupAudioTracks(final Activity activity, final DRMContent drmContent, final Spinner spinnerAudioStreams, final TextView spinnerAudioStreamsLabel)
    {
        final List<DRMContent.AudioTrack> audioTracks = drmContent.getAudioTracks();

        if (audioTracks.size() > 1)
        {
            //audio
            spinnerAudioStreamsLabel.setVisibility(View.VISIBLE);
            ArrayAdapter<CharSequence> adapterSpinnerAudioStreams = new ArrayAdapter<CharSequence>(activity, android.R.layout.simple_spinner_item);
            spinnerAudioStreams.setVisibility(View.VISIBLE);
            spinnerAudioStreams.setAdapter(adapterSpinnerAudioStreams);

            DRMContent.AudioTrack selectedAudioTrack = drmContent.getAudioTrack();
            for (DRMContent.AudioTrack audioTrack : audioTracks)
            {
                adapterSpinnerAudioStreams.add(audioTrack.mName);
                if (audioTracks.indexOf(selectedAudioTrack) == audioTracks.indexOf(audioTrack))
                {
                    spinnerAudioStreams.setSelection(audioTracks.indexOf(selectedAudioTrack));
                }
            }

            adapterSpinnerAudioStreams.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerAudioStreams.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
            {
                public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id)
                {
                    final DRMContent.AudioTrack selectedAudioTrack = audioTracks.get(position);
                    final DRMContent.AudioTrack audioTrack = drmContent.getAudioTrack();
                    if (!AudioTrackHelper.isSame(selectedAudioTrack, audioTrack))
                    {
                        mMediaPlayer.setAudioTrack(selectedAudioTrack);
                    }
                }

                public void onNothingSelected(final AdapterView<?> parent)
                {

                }
            });
            return true;
        }
        else
        {
            spinnerAudioStreams.setVisibility(View.GONE);
            spinnerAudioStreamsLabel.setVisibility(View.GONE);
            return false;
        }
    }

    public void toggleVisibility(int visibility)
    {
        if (mCCAudioBoxNeeded)
        {
            mCCAudioBox.setVisibility(visibility);
        }

    }


    @Override
    public void onSubtitle(final MediaPlayer mediaPlayer, final Subtitle subtitle)
    {
        mSubtitleHandler.addSubtitle(subtitle);
    }
}
