package com.ttiglobal.inttilectgpchat.blog;

import android.annotation.SuppressLint;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.ttiglobal.inttilectgpchat.R;
import com.ttiglobal.inttilectgpchat.videoview.UniversalMediaController;
import com.ttiglobal.inttilectgpchat.videoview.UniversalVideoView;
import com.ttiglobal.inttilectgpchat.wrappers.MyContent;
import com.ttiglobal.inttilectgpchat.wrappers.MyTopic;
import com.ttiglobal.inttilectgpchat.wrappers.TrainingCategory;

public class VideoView extends AppCompatActivity {

    public static MyContent CONTENT = new MyContent();
    public static MyTopic TOPIC = new MyTopic();
    public static TrainingCategory TRAINING_ID;
    public int total;
    TextView title,post_desc,post_title;
    UniversalVideoView videoView;
    UniversalMediaController mMediaController;
    View mBottomLayout;
    View mVideoLayout;
    View appBarLayout;
    Toolbar toolbar;
    private int mSeekPosition;
    private int cachedHeight;
    ProgressBar progressBar;
    private boolean isFullscreenMode;
    private String filePath;
    private Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_view);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            filePath = extras.getString("filepath");
        }
        mVideoLayout = findViewById(R.id.video_layout);
        mBottomLayout = findViewById(R.id.bottom_layout);
        appBarLayout = findViewById(R.id.appBarLayout);
        mMediaController = findViewById(R.id.media_controller);
        title = findViewById(R.id.title);
        videoView = findViewById(R.id.videoView);
        mVideoLayout = findViewById(R.id.video_layout);
        post_desc = findViewById(R.id.post_desc);
        post_title=findViewById(R.id.post_title);

        bundle = getIntent().getExtras();
        String Title = bundle.getString("blog_title");
        String Details = bundle.getString("blog_desc");

//        post_title.setText(Title);
        post_desc.setText(Details);

        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setContentInsetStartWithNavigation(0);
//        toolbar.setTitle("VIDEO BLOG");
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        @SuppressLint("PrivateResource")
        Drawable backArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_material);
        backArrow.setColorFilter(getResources().getColor(R.color.txt_color), PorterDuff.Mode.SRC_ATOP);
        getSupportActionBar().setHomeAsUpIndicator(backArrow);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                loadVideo();
            }
        }, 1000);
    }

    public void loadVideo() {
         mVideoLayout.post(new Runnable() {
                @Override
                public void run() {
                    int width = mVideoLayout.getWidth();
                    cachedHeight = (int) (width * 405f / 720f);
                    ViewGroup.LayoutParams videoLayoutParams = mVideoLayout.getLayoutParams();
                    videoLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                    videoLayoutParams.height = cachedHeight;
                    mVideoLayout.setLayoutParams(videoLayoutParams);
                    videoView.setVideoPath(filePath);
                    videoView.requestFocus();
                    videoView.start();
                    mMediaController.setTitle(CONTENT.getTitle());
                    videoView.setMediaController(mMediaController);
                    videoView.setVideoViewCallback(new UniversalVideoView.VideoViewCallback() {
                        @Override
                        public void onScaleChange(boolean isFullscreen) {
                            isFullscreenMode = isFullscreen;
                            if (isFullscreen) {
                                ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams();
                                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                                layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                                mVideoLayout.setLayoutParams(layoutParams);
                                mBottomLayout.setVisibility(View.GONE);
                            } else {
                                ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams();
                                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                                layoutParams.height = cachedHeight;
                                mVideoLayout.setLayoutParams(layoutParams);
                                mBottomLayout.setVisibility(View.VISIBLE);
                            }
                            switchTitleBar(!isFullscreen);
                        }

                        @Override
                        public void onPause(MediaPlayer mediaPlayer) {
                            if (videoView != null && videoView.isPlaying()) {
                                mSeekPosition = videoView.getCurrentPosition();
                                videoView.pause();
                            }
                        }

                        @Override
                        public void onStart(MediaPlayer mediaPlayer) {
                     }

                        @Override
                        public void onBufferingStart(MediaPlayer mediaPlayer) {
                            progressBar.setVisibility(View.GONE);
                        }

                        @Override
                        public void onBufferingEnd(MediaPlayer mediaPlayer) {

                        }
                    });
                }
            });
    }

    private void switchTitleBar(boolean show) {
        androidx.appcompat.app.ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            if (show) {
                supportActionBar.show();
            } else {
                supportActionBar.hide();
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (isFullscreenMode) {
            videoView.setFullscreen(false);
        } else {
            super.onBackPressed();
            this.overridePendingTransition(R.anim.animation_f_enter,
                    R.anim.animation_f_leave);
        }
    }
}
