package com.example.onetap_ssh;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.onetap_ssh.databinding.ActivityIntroBinding;
import com.example.onetap_ssh.unlock.UnlockActivity;
import com.example.onetap_ssh.util.SettingsUtil;

public class IntroActivity extends BaseActivity {

    public static boolean show_logoanimation = false;

    private ActivityIntroBinding binding;

    private MediaPlayer mMediaPlayer;

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

        if (!SettingsUtil.isIntroVideoEnabled(this)) {
            openMainActivity();
            return;
        }

        binding = ActivityIntroBinding.inflate(getLayoutInflater());

        Uri uri = Uri.parse(String.format("android.resource://%s/%s", getPackageName(), R.raw.intro_vp9));
        binding.videoView.setVideoURI(uri);
        binding.videoView.start();

        binding.videoView.setOnPreparedListener(mediaPlayer -> {
            mMediaPlayer = mediaPlayer;
            setDimension();
        });

        binding.videoView.setOnCompletionListener(mp -> openMainActivity());

        binding.videoView.setOnErrorListener((MediaPlayer mp, int what, int extra) -> {
            Toast.makeText(this, R.string.intro_video_failed, Toast.LENGTH_LONG).show();
            openMainActivity();
            return true;
        });

        setContentView(binding.getRoot());

        SharedPreferences p = getSharedPreferences("appsettings", Activity.MODE_PRIVATE);
        show_logoanimation = p.getBoolean("Logoanimation", true);
        if (show_logoanimation) {
            setContentView(R.layout.activity_intro);
            final Handler handler = new Handler(Looper.getMainLooper());
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //Do something after 4000ms
                    Intent m = new Intent(getApplicationContext(), MainActivity.class);
                    m.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivity(m);
                }
            }, 4000);
        } else {
            Intent m = new Intent(getApplicationContext(), MainActivity.class);
            m.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(m);
        }
    }

    public void openMainActivity() {
        Intent m = new Intent(getApplicationContext(), UnlockActivity.class);
        m.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(m);
        finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mMediaPlayer != null) mMediaPlayer.release();
        mMediaPlayer = null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.videoView.start();
    }

    private void setDimension() {
        float videoProportion = (float) mMediaPlayer.getVideoHeight() / mMediaPlayer.getVideoWidth();
        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        int screenHeight = getResources().getDisplayMetrics().heightPixels;
        float screenProportion = (float) screenHeight / (float) screenWidth;
        ViewGroup.LayoutParams lp = binding.videoView.getLayoutParams();

        if (videoProportion < screenProportion) {
            lp.height= screenHeight;
            lp.width = (int) ((float) screenHeight / videoProportion);
        } else {
            lp.width = screenWidth;
            lp.height = (int) ((float) screenWidth * videoProportion);
        }
        binding.videoView.setLayoutParams(lp);
    }


}