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

import android.content.Intent;
import android.util.Pair;
import android.view.View;

import androidx.annotation.OptIn;
import androidx.lifecycle.Observer;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.offline.Download;

import com.bumptech.glide.Glide;
import com.hi.music.player.R;
import com.hi.music.player.api.LikeSongListener;
import com.hi.music.player.databinding.FragmentProfileBinding;
import com.hi.music.player.helper.CommonUtils;
import com.hi.music.player.helper.MyValue;
import com.hi.music.player.javabean.BoxDownloadSong;
import com.hi.music.player.javabean.BoxLikeSong;
import com.hi.music.player.media3.MyDownloadService;
import com.hi.music.player.objectbox.ObjectBoxManager;
import com.hi.music.player.ui.activity.LikeSongActivity;
import com.hi.music.player.ui.activity.viewmodel.VMApplication;

import java.util.List;

import io.objectbox.reactive.DataSubscription;


public class ProfileFragment extends BaseFragment<FragmentProfileBinding> implements View.OnClickListener {


    private DataSubscription dataSubscription;

    private int likeSongCount = 0;
    private int downloadSongCount = 0;



    @Override
    protected FragmentProfileBinding getFragmentVb() {
        return FragmentProfileBinding.inflate(getLayoutInflater());
    }


    @OptIn(markerClass = UnstableApi.class)
    @Override
    protected void initView() {

        dataSubscription = ObjectBoxManager.setLikeDataListener(new LikeSongListener() {
            @Override
            public void onLikeSongChange(List<BoxLikeSong> data) {
                CommonUtils.LogMsg("------onLikeSongChange  data=" + data.size());
                int size = data.size();
                likeSongCount = size;
                Vb.tvLikeSize.setText(String.format(getString(R.string.like_song), size));
                if (size == 0) {
                    Vb.likeCovert.setVisibility(View.GONE);
                    Vb.likeDefault.setVisibility(View.VISIBLE);
                } else {
                    Vb.likeCovert.setVisibility(View.VISIBLE);
                    Vb.likeDefault.setVisibility(View.GONE);
                    BoxLikeSong boxLikeSong = data.get(size - 1);
                    Glide.with(requireContext())
                            .asDrawable()
                            .load(boxLikeSong.getCovert())
                            .placeholder(R.mipmap.im_placeholder)
                            .error(R.mipmap.im_placeholder)
                            .into(Vb.likeCovert);
                }
            }
        });
        MyDownloadService.updateDownloadUi(vmApplication);

        vmApplication.downloadData.observe(getViewLifecycleOwner(), new Observer<List<Download>>() {
            @Override
            public void onChanged(List<Download> downloads) {
                int size = downloads.size();
                Vb.tvDownloadSize.setText(String.format(getString(R.string.download_song), size));
                downloadSongCount = size;
                if (size > 0) {
                    Download download = downloads.get(downloads.size() - 1);
                    BoxDownloadSong boxDownloadSong = CommonUtils.downloadToBean(download);

                    Vb.downloadCovert.setVisibility(View.VISIBLE);
                    Vb.downloadDefault.setVisibility(View.GONE);
                    Glide.with(requireContext())
                            .asDrawable()
                            .load(boxDownloadSong.getCovert())
                            .placeholder(R.mipmap.im_placeholder)
                            .error(R.mipmap.im_placeholder)
                            .into(Vb.downloadCovert);
                } else {
                    Vb.downloadCovert.setVisibility(View.GONE);
                    Vb.downloadDefault.setVisibility(View.VISIBLE);
                }
            }
        });

        Vb.relayoutLike.setOnClickListener(this);
        Vb.relayoutDownload.setOnClickListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        CommonUtils.LogMsg("------ProfileFragment-onStart");

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (dataSubscription != null) {
            dataSubscription.cancel();
        }
        CommonUtils.LogMsg("------ProfileFragment-onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        CommonUtils.LogMsg("------ProfileFragment-onDestroy");
    }

    @Override
    public void onClick(View v) {
        if (v.equals(Vb.relayoutLike)) {
            if (likeSongCount > 0) {
                Intent intent = new Intent(requireActivity(), LikeSongActivity.class);
                intent.putExtra(MyValue.KEY_ENTER_LIKE_ACTIVITY_TYPE, MyValue.KEY_ENTER_LIKE_ACTIVITY_TYPE_LIKE);
                startActivity(intent);
            }
        } else if (v.equals(Vb.relayoutDownload)) {
            if (downloadSongCount > 0) {
                Intent intent = new Intent(requireActivity(), LikeSongActivity.class);
                intent.putExtra(MyValue.KEY_ENTER_LIKE_ACTIVITY_TYPE, MyValue.KEY_ENTER_LIKE_ACTIVITY_TYPE_DOWNLOAD);
                startActivity(intent);
            }
        }

    }
}