package com.offline.music.playermp3.ui.activity;

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 androidx.recyclerview.widget.LinearLayoutManager;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.nativeAds.MaxNativeAdLoader;
import com.offline.music.playermp3.R;
import com.offline.music.playermp3.adapter.AdapterDownloadSong;
import com.offline.music.playermp3.adapter.AdapterLikeSong;
import com.offline.music.playermp3.api.HomeItemClickListener;
import com.offline.music.playermp3.databinding.ActivityLikeSongBinding;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.helper.MyValue;
import com.offline.music.playermp3.javabean.BoxLikeSong;
import com.offline.music.playermp3.javabean.response.ResponsePlayListInfo;
import com.offline.music.playermp3.max.MaxManager;
import com.offline.music.playermp3.max.NativeMaxListener;
import com.offline.music.playermp3.media3.MyDownloadService;
import com.offline.music.playermp3.objectbox.ObjectBoxManager;

import java.util.ArrayList;
import java.util.List;

public class LikeSongActivity extends BaseActivity<ActivityLikeSongBinding> implements HomeItemClickListener {


    private List<BoxLikeSong> boxLikeSongs;
    private int mType;
    private MaxAd nativeAd;
    private MaxNativeAdLoader nativeAdLoader;
    private List<Pair<Integer, String>> downloadList = new ArrayList<>();

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

    @OptIn(markerClass = UnstableApi.class)
    @Override
    protected void onCreateInit() {
        Intent intent = getIntent();
        mType = intent.getIntExtra(MyValue.KEY_ENTER_LIKE_ACTIVITY_TYPE, MyValue.KEY_ENTER_LIKE_ACTIVITY_TYPE_LIKE);

        MaxManager.onLoadNativeAd(vb.adLayout, MaxManager.native_Ad2, new NativeMaxListener() {
            @Override
            public void onLoaded(MaxAd ad, MaxNativeAdLoader maxNativeAdLoader) {
                nativeAd = ad;
                nativeAdLoader = maxNativeAdLoader;
            }
        });

        switch (mType) {
            case MyValue.KEY_ENTER_LIKE_ACTIVITY_TYPE_LIKE:
                vb.tvTitle.setText(getString(R.string.text_like_song));
                boxLikeSongs = ObjectBoxManager.queryAllLike();
                AdapterLikeSong adapterLikeSong = new AdapterLikeSong(this, vmApplication);
                adapterLikeSong.setHomeItemClickListener(this);
                adapterLikeSong.setData(boxLikeSongs);
                vb.recycler.setLayoutManager(new LinearLayoutManager(this));
                vb.recycler.setAdapter(adapterLikeSong);
                vb.tvSongSize.setText(String.format(getString(R.string.like_song), boxLikeSongs.size()));
                MyDownloadService.addDownloadListener(vmApplication);
                vmApplication.downloadChange.observe(this, new Observer<Pair<Boolean, Download>>() {
                    @Override
                    public void onChanged(Pair<Boolean, Download> booleanDownloadPair) {
                        Boolean first = booleanDownloadPair.first;
                        Download downloads = booleanDownloadPair.second;

                        String id = downloads.request.id;
                        for (Pair<Integer,String> pair:downloadList){
                            Integer position = pair.first;
                            String videoId = pair.second;
                            if(id.equals(videoId)){
                                adapterLikeSong.updateDownloadStatus(first,position,videoId);
                            }
                        }
                    }
                });
                break;

            case MyValue.KEY_ENTER_LIKE_ACTIVITY_TYPE_DOWNLOAD:
                vb.tvTitle.setText(getString(R.string.text_offline_song));
                MyDownloadService.updateDownloadUi(vmApplication);
                vmApplication.downloadData.observe(this, new Observer<List<Download>>() {
                    @Override
                    public void onChanged(List<Download> downloads) {
                        AdapterDownloadSong adapterDownloadSong = new AdapterDownloadSong();
                        adapterDownloadSong.setHomeItemClickListener(LikeSongActivity.this);
                        adapterDownloadSong.setData(downloads);
                        vb.recycler.setLayoutManager(new LinearLayoutManager(LikeSongActivity.this));
                        vb.recycler.setAdapter(adapterDownloadSong);
                        vb.tvSongSize.setText(String.format(getString(R.string.download_song), downloads.size()));
                    }
                });
                break;
        }


    }

    @Override
    protected void onDestroy() {
        if(nativeAd!= null&&nativeAdLoader!= null){
            MaxManager.clearNativeAd(nativeAd,nativeAdLoader);
        }
        super.onDestroy();
    }

    @Override
    protected void onInitClick() {
        vb.imBack.setOnClickListener(this);
    }

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

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

    @Override
    public boolean showPanel() {
        return true;
    }

    @Override
    public void onClick(View v) {
        if (v.equals(vb.imBack)) {
            finish();
        }
    }

    @Override
    public void onClickLikeSong(BoxLikeSong boxLikeSong, int index) {
        List<ResponsePlayListInfo> playList = new ArrayList<>();
        for (BoxLikeSong song : boxLikeSongs) {
            String videoId1 = song.getVideoId();
            String songName = song.getSongName();
            String covert = song.getCovert();
            String singerName = song.getSingerName();
            long durationMs = song.getDurationMs();
            String duration = song.getDuration();

            ResponsePlayListInfo playListInfo = new ResponsePlayListInfo(covert, songName, singerName, durationMs, videoId1, duration);
            playList.add(playListInfo);
        }
        vmApplication.reSetPlayList(playList);

        String videoId = boxLikeSong.getVideoId();

        Intent intent = new Intent(this, PlayActivity.class);
        intent.putExtra(MyValue.KEY_PLAY_VIDEO_ID, videoId);
        intent.putExtra(MyValue.KEY_PLAY_INDEX, index);
        intent.putExtra(MyValue.KEY_ENTER_SOURCE, MyValue.TYPE_ENTER_LIKE);
        startActivity(intent);


    }

    @OptIn(markerClass = UnstableApi.class)
    @Override
    public void onClickDownloadSong(Download download, int index) {

//        MyDownloadService.updateDownloadUi(vmApplication)

        String videoId = download.request.id;

        Intent intent = new Intent(this, PlayActivity.class);
        intent.putExtra(MyValue.KEY_PLAY_VIDEO_ID, videoId);
        intent.putExtra(MyValue.KEY_PLAY_INDEX, index);
        intent.putExtra(MyValue.KEY_ENTER_SOURCE, MyValue.TYPE_ENTER_DOWNLOAD);
        startActivity(intent);
    }

    @Override
    public void onDownloadSong(String videoId, int index) {
        Pair<Integer, String> integerStringPair = new Pair<>(index, videoId);
        downloadList.add(integerStringPair);
    }
}