package com.lc.myapplication.security.mysoundstwo.activity;

import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.lc.myapplication.security.mysoundstwo.R;
import com.lc.myapplication.security.mysoundstwo.list.SoundsData;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeDatabase;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeEntity;
import com.lc.myapplication.security.mysoundstwo.tool.MyTool;
import com.lc.myapplication.security.mysoundstwo.value.MyValue;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

public class PreviewActivity extends AppCompatActivity {

    private ImageView preview_back;
    private ImageView preview_image;
    private TextView preview_name;
    private Button preview_play;
    private ImageView preview_like;
    private boolean isplay = false;
    private String urlmp3;
    private ScaryPrankLikeEntity list;
    private MediaPlayer mediaPlayer;
    private SeekBar preview_seekbar;
    private AudioManager audioManager;
    private ImageView preview_re_song;
    private RelativeLayout preview_main;
    private int bg;
    private boolean islike = false;
    private int c = 0;
    private String name;
    private String image;
    private boolean resongs = false;
    private SoundsData listdata;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview);
        audioManager =(AudioManager)getSystemService(AUDIO_SERVICE);
        gotofindview();
        setbackground();
        gotoGetData();
        onMyClick();
    }

    private void gotofindview(){
        preview_main = findViewById(R.id.preview_main);
        preview_back = findViewById(R.id.preview_back);
        preview_image = findViewById(R.id.preview_image);
        preview_name = findViewById(R.id.preview_name);
        preview_name.setTypeface(Typeface.createFromAsset(getAssets(),"myfront1.ttf"));
        preview_re_song = findViewById(R.id.preview_re_song);
        preview_play = findViewById(R.id.preview_play);
        preview_like = findViewById(R.id.preview_like);
        preview_seekbar = findViewById(R.id.preview_seekbar);
        preview_seekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
        //监听进度条滑动事件
        preview_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
    private void setbackground() {
        Window window = getWindow();
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        preview_main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
    }

    private void onMyClick() {
        preview_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        preview_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isplay){
                    isplay = false;
                    preview_play.setBackgroundResource(R.drawable.sounds_play_bg);
                    if (mediaPlayer != null){
                        mediaPlayer.release();
                        mediaPlayer = null;
                    }
                }else {
                    preview_play.setBackgroundResource(R.drawable.sounds_stop_bg);
                    mediaPlayer = new MediaPlayer();
                    try {
                        mediaPlayer.setDataSource(urlmp3);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            //Log.d("---------","--------finish");
                            mediaPlayer.start();
                        }
                    });
                    isplay = true;
                    mediaPlayer.prepareAsync();
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            if (resongs){
                               mediaPlayer.start();
                            }else {
                                mediaPlayer.release();
                                mediaPlayer = null;
                                preview_play.setBackgroundResource(R.drawable.sounds_play_bg);
                            }
                        }
                    });
                }
            }
        });
        preview_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (islike){
                    islike = false;
                    preview_like.setBackgroundResource(R.drawable.unlike_bg);
                    list.setSoundslike(false);
                }else {
                    islike = true;
                    preview_like.setBackgroundResource(R.drawable.like_bg);
                    list.setSoundslike(true);
                }
                MyTool.RunIO(new Runnable() {
                    @Override
                    public void run() {
                        ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().UpdateLikeSounds(list);
                    }
                });
            }
        });
        preview_re_song.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(resongs){
                    preview_re_song.setBackgroundResource(R.drawable.no_recycle);
                    resongs = false;
                }else {
                    preview_re_song.setBackgroundResource(R.drawable.recycle_bg);
                    resongs = true;
                }
            }
        });
    }
    private void gotoGetData() {
        Intent intent = getIntent();
        name = intent.getStringExtra(MyValue.DATA_NAME);
        //Log.d("dhncatname2","---"+name);
        MyTool.RunIO(new Runnable() {
            @Override
            public void run() {
                list = ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().QueryLikeName(name);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        image = list.getImage();
                        //Log.d("dhncatname3","---"+list);
                        //Log.d("dhncatname4","---"+list.getImage());
                        //Log.d("---------","--------mp3"+image);
                        if (Objects.equals(list.getResource(), 0)){
                            // Log.d("----1","--------mp3"+urlmp3);
                            Glide.with(PreviewActivity.this)
                                    .load(image)
                                    .listener(new RequestListener<Drawable>() {
                                        @Override
                                        public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                                            return false;
                                        }

                                        @Override
                                        public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                                            return false;
                                        }
                                    })
                                    .into(preview_image);
                        }else {
                            //Log.d("----2","--------mp3"+urlmp3);
                            preview_image.setBackgroundResource(R.mipmap.creat_image);
                        }
                        urlmp3 = list.getMp3url();
                        //Log.d("---------","--------mp3"+urlmp3);
                        preview_name.setText(name);
                        MyTool.RunIO(new Runnable() {
                            @Override
                            public void run() {
                                list = ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().QueryLikeName(name);
                            }
                        });
                        if (list.getSoundslike()){
                            preview_like.setBackgroundResource(R.drawable.like_bg);
                            islike = true;
                        }else {
                            preview_like.setBackgroundResource(R.drawable.unlike_bg);
                            islike = false;
                        }
                    }
                });
            }
        });
        String getbg = intent.getStringExtra(MyValue.DATA_BG);
        bg = Integer.parseInt(getbg);
        preview_main.setBackgroundResource(bg);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        gotoGetData();
    }
}