package macampcorp.macamp.activities.viewers;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import macampcorp.macamp.activities.R;
import macampcorp.macamp.exceptions.BadResourceTypeException;
import macampcorp.macamp.exceptions.ResourceFileNotFoundException;
import macampcorp.macamp.ui.MediaView;
import macampcorp.macamp.utils.FontChangeCrawler;
import macampcorp.macamp.game.Resource;
import macampcorp.macamp.utils.files.FileManager;

public class MediaViewerAcitvity extends AppCompatActivity implements MediaPlayer.OnSeekCompleteListener,
                                                                MediaView.OnStartListener,
                                                                SeekBar.OnSeekBarChangeListener {
    public static final int REQUEST_CODE = 27;
    private Resource mResource;
    private Button mInfo;
    private TextView mTitle;

    private FrameLayout mFrameLayout;
    private ImageButton  mButton;
    private MediaView mMediaView;
    private ImageView mImageView;
    private SeekBar mBar;
    private Handler mHandler = new Handler();
    private AutoSeek mAutoSeek;
    private int mCurrentTime;
    private int mEndTime;
    private boolean mUserSeeks=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_media);

        mFrameLayout = (FrameLayout) findViewById(R.id.media_viewer);
        mButton = (ImageButton) findViewById(R.id.play);
        mInfo = (Button) findViewById(R.id.info);
        mBar = (SeekBar) findViewById(R.id.seekBar);
        mBar.setOnSeekBarChangeListener(this);

        mTitle = (TextView) findViewById(R.id.title);
        mAutoSeek = new AutoSeek(mHandler);

        mMediaView = new MediaView(this);
        mMediaView.setOnStartListener(this);
        mImageView = new ImageView(this);

        mFrameLayout.addView(mMediaView);
        mFrameLayout.addView(mImageView);


        if(getIntent().hasExtra("resource"))
        {
            mResource = (Resource) getIntent().getSerializableExtra("resource");
            if(mResource==null)
            {
                throw  new RuntimeException("Ressource non valide");
            }

            mTitle.setText(mResource.getTitle());

            String path=mResource.getFile();
            if(mResource.isAudio())
            {
                mImageView.setVisibility(View.VISIBLE);
                String image = mResource.getimage();
                if(image!=null)
                {
                    mImageView.setImageBitmap(FileManager.openImage(this, image));
                }
            }
            else if (mResource.isVideo())
            {
                mImageView.setVisibility(View.GONE);
            }else
            {
                throw new BadResourceTypeException(mResource, Resource.AUDIO+" ou "+Resource.VIDEO);
            }

            boolean b = mMediaView.play(path);
            if(!b) throw new ResourceFileNotFoundException(mResource);
        }


    }

    public void onFullscreen(View v)
    {
        if(mTitle.getVisibility()==View.VISIBLE)
        {
            mTitle.setVisibility(View.GONE);
            mInfo.setVisibility(View.GONE);
        }else
        {
            mTitle.setVisibility(View.VISIBLE);
            mInfo.setVisibility(View.VISIBLE);
        }
    }

    public void onPlayPause(View v)
    {
        if(mMediaView.isPlaying())
        {
            mAutoSeek.stop();
            mButton.setImageResource(android.R.drawable.ic_media_play);
            mMediaView.pause();
        }else
        {
            mAutoSeek.start();
            mButton.setImageResource(android.R.drawable.ic_media_pause);
            mMediaView.start();
        }
    }

    public void onOk(View v)
    {
        setResult(RESULT_OK, new Intent());
        finish();
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_OK, new Intent());
        super.onBackPressed();
    }

    @Override
    public void onSeekComplete(MediaPlayer mediaPlayer) {
        mAutoSeek.start();
        mButton.setImageResource(android.R.drawable.ic_media_pause);
    }



    public void onUpdateSeek()
    {
        if(mMediaView.isPlaying())
        {
            mCurrentTime=mMediaView.getTime()/1000;
            mBar.setProgress(mCurrentTime);
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        mUserSeeks=b;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }


    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if(mUserSeeks)
        {
            mCurrentTime=seekBar.getProgress();
            mMediaView.seek(mCurrentTime*1000);
            mAutoSeek.stop();
            mUserSeeks=false;
        }
    }

    @Override
    public void onStart(MediaView mv) {
        mCurrentTime=mv.getTime()/1000;
        mEndTime=mv.getDuration()/1000;

        mBar.setMax(mEndTime);
        mBar.setProgress(mCurrentTime);
        mMediaView.setOnSeekCompleteListener(this);

        mAutoSeek.start();
        mButton.setImageResource(android.R.drawable.ic_media_pause);
    }

    private class AutoSeek implements Runnable
    {
        private Handler mHandler;
        AutoSeek(Handler h)
        {
            mHandler=h;
        }

        @Override
        public void run() {
            onUpdateSeek();
            mHandler.postDelayed(this, 500);
        }

        public void start()
        {
            mHandler.postDelayed(this, 500);
        }


        public void stop()
        {
            mHandler.removeCallbacks(this);
        }
    }

    protected void onResume()
    {
        super.onResume();
    }

    protected  void onPause()
    {
        mMediaView.stop();
        mAutoSeek.stop();
        super.onPause();

    }

    public void finish()
    {

        mMediaView.stop();
        mAutoSeek.stop();
        mMediaView.clearPlayer();
        super.finish();
    }

    public void onDestroy()
    {
        mMediaView.stop();
        mAutoSeek.stop();
        mMediaView.clearPlayer();
        super.onDestroy();
    }

}
