package com.hi.music.player.ui.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.hi.music.player.MusicApplication;
import com.hi.music.player.R;
import com.hi.music.player.databinding.ActivityPlayBinding;
import com.hi.music.player.helper.CommonUtils;
import com.hi.music.player.helper.MyValue;
import com.hi.music.player.javabean.response.ResponsePlay;
import com.hi.music.player.javabean.response.child.ResponseSingle;
import com.hi.music.player.media3.MyMediaControllerManager;
import com.hi.music.player.ui.activity.viewmodel.VMPlay;

import java.util.List;

public class PlayActivity extends BaseActivity<ActivityPlayBinding> {


    private ResponseSingle responseSingle;
    private VMPlay vmPlay;
    private MyMediaControllerManager mediaController;

    @Override
    protected ActivityPlayBinding getViewBinding() {
        return ActivityPlayBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onCreateInit() {
        Intent intent = getIntent();
        responseSingle = (ResponseSingle) intent.getSerializableExtra(MyValue.KEY_PLAY_ACTIVITY_SINGER);
        vmPlay = getActivityScopeViewModel(VMPlay.class);
        vmPlay.getPlay(responseSingle);

        vmPlay.data.observe(this, new Observer<List<ResponsePlay>>() {
            @Override
            public void onChanged(List<ResponsePlay> playList) {
                if (playList.size() > 0) {
                    ResponsePlay responsePlay = playList.get(0);
                    loadCovert(responsePlay.getCovert());
                    loadInfo(responsePlay);

                }

            }
        });

    }

    @Override
    protected void onInitClick() {
        vb.btnPlay.setOnClickListener(this::onClick);
    }

    private void loadInfo(ResponsePlay data) {
        vb.tvSongName.setText(data.getSongTitle());
        vb.tvSingerName.setText(data.getSingerName());
        vb.tvDuration.setText(data.getDuration());

        mediaController = MyMediaControllerManager.getInstance();
        mediaController.init();
        mediaController.addMusicPlay(data);
    }

    private void loadCovert(String url) {
        Glide.with(MusicApplication.myApplication)
                .asDrawable()
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(CommonUtils.dpToPx(16))))
                .load(url)
                .placeholder(R.mipmap.ic_launcher)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        CommonUtils.LogMsg(e.getMessage());
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(vb.imCovert);
    }



    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public boolean isFullScreen() {
        return false;
    }

    @Override
    public boolean statusBarLight() {
        return false;
    }

    @Override
    public void onClick(View v) {
        if(v.equals(vb.btnPlay)){
            mediaController.play();
        }

    }
}