package com.hesiii.i_hear.HearingScreening;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;

import com.hesiii.i_hear.Models.Status;
import com.hesiii.i_hear.R;
import com.hesiii.i_hear.databinding.ActivityTestBinding;

import java.util.ArrayList;

public class TestActivity extends AppCompatActivity {

    private ActivityTestBinding binding;
    int [] musics;
    private MediaPlayer mediaPlayer;
    private final Handler handler = new Handler();
    private int currentPosition = 0;
    private ArrayList<Status> resultList;

    private boolean firstTone = false;
    private boolean secondTone = false;
    private boolean thirdTone = false;
    private boolean fourthTone = false;
    private String status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTestBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        if (getSupportActionBar() != null ){

            getSupportActionBar().hide();

        }

        musics = new int[] {R.raw.a, R.raw.b, R.raw.c, R.raw.d, R.raw.e, R.raw.f, R.raw.g};
        resultList = new ArrayList<>();

        binding.startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startButtonProcess();

            }
        });

        binding.notHearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (currentPosition == 0) {

                    firstTone = false;
                    //resultList.add(10);
                    resultList.add(new Status(10, 125));

                } else if (currentPosition == 1) {

                    secondTone = false;
                    //resultList.add(30);
                    resultList.add(new Status(30, 250));


                } else if (currentPosition == 2) {

                    thirdTone = false;
                    //resultList.add(60);
                    resultList.add(new Status(60, 500));

                } else if (currentPosition == 3) {

                    fourthTone = false;
                    //resultList.add(80);
                    resultList.add(new Status(80, 1000));

                } else if (currentPosition == 4) {

                    //resultList.add(100);
                    resultList.add(new Status(100, 2000));

                }

                heardButtonProcess();

            }
        });

        binding.heardButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (currentPosition == 0) {

                    firstTone = true;
                    //resultList.add(0);
                    resultList.add(new Status(0, 125));

                } else if (currentPosition == 1) {

                    secondTone = true;
                    //resultList.add(20);
                    resultList.add(new Status(20, 250));


                } else if (currentPosition == 2) {

                    thirdTone = true;
                    //resultList.add(40);
                    resultList.add(new Status(40, 500));

                } else if (currentPosition == 3) {

                    fourthTone = true;
                    //resultList.add(70);
                    resultList.add(new Status(70, 1000));

                } else if (currentPosition == 4) {

                    //resultList.add(90);
                    resultList.add(new Status(90, 2000));

                }

                heardButtonProcess();

            }
        });

    }

    private void heardButtonProcess() {

        currentPosition++;

        if (currentPosition != 5) {

            startButtonProcess();
            System.out.println("currentPosition "+currentPosition);

        } else {

//            for (int i = 0; i < resultList.size(); i++) {
//
//                System.out.println(String.valueOf(i+1)+" Result - "+resultList.get(i));
//
//            }

            if (firstTone || secondTone) {

                status = "Normal Hearing Level";

            } else if (thirdTone || fourthTone) {

                status = "Mild to Moderate Level";

            } else {

                status = "Profound Level";

            }

            Intent intent = new Intent(this, ResultActivity.class);
            intent.putExtra("resultList", resultList);
            intent.putExtra("status", status);
            startActivity(intent);

        }
    }

    private void startButtonProcess() {

        mediaPlayer = null;

        binding.startButton.setVisibility(View.GONE);
        mediaPlayer = MediaPlayer.create(this, musics[currentPosition]);
        //mediaPlayer.setLooping(true);
        mediaPlayer.setVolume(1.0f, 1.0f);
        binding.musicControlLayout.setVisibility(View.VISIBLE);
        binding.seekBar.setProgress(0);
        binding.seekBar.setMax(mediaPlayer.getDuration());

        //mediaPlayer.prepare();
        mediaPlayer.start();

        String test = "Test " + String.valueOf(currentPosition + 1);
        binding.testCount.setText(test);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (mediaPlayer != null) {

                    binding.seekBar.setProgress(mediaPlayer.getCurrentPosition());

                }


                new Handler().postDelayed(this, 100);

            }
        });

        binding.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (mediaPlayer != null && b) {

                    mediaPlayer.seekTo(i);

                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
                binding.seekBar.setProgress(0);
                System.out.println("setOnCompletionListener");
                // Update UI or perform any other actions after music ends
            }
        });

    }
}