package com.offline.music.playermp3.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 androidx.recyclerview.widget.LinearLayoutManager;

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.offline.music.playermp3.R;
import com.offline.music.playermp3.adapter.AdapterResult;
import com.offline.music.playermp3.api.HomeItemClickListener;
import com.offline.music.playermp3.databinding.ActivityResultListBinding;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.helper.MyValue;
import com.offline.music.playermp3.javabean.response.ResponseResult;
import com.offline.music.playermp3.javabean.response.child.ResponseResultListChild;
import com.offline.music.playermp3.javabean.response.child.ResponseSingle;
import com.offline.music.playermp3.ui.fragmnt.viewmodel.VMResultList;

public class ResultListActivity extends BaseActivity<ActivityResultListBinding> implements HomeItemClickListener {
    private VMResultList vm;
    private String key;

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

    @Override
    protected void onCreateInit() {
        vm = getActivityScopeViewModel(VMResultList.class);
        key = getIntent().getStringExtra(MyValue.KEY_SEARCH_RESULT_BROWSER_ID);
        vm.getList(key);

        vm.data.observe(this, new Observer<ResponseResult>() {
            @Override
            public void onChanged(ResponseResult responseResult) {
                vb.pbLoading.setVisibility(View.GONE);
                if (responseResult == null) {
                    vb.layoutError.linearRetry.setVisibility(View.VISIBLE);
                    return;
                }
                vb.pbLoading.setVisibility(View.GONE);
                loadInfo(responseResult);
            }
        });

    }

    private void loadInfo(ResponseResult result) {
        Glide.with(this)
                .asDrawable()
                .load(result.getMainCovert())
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(CommonUtils.dpToPx(4))))
                .placeholder(R.mipmap.im_placeholder)
                .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.covert);
        vb.mainTitle.setText(result.getMainTitle());
        AdapterResult adapterResult = new AdapterResult(this);
        adapterResult.setHomeItemClickListener(this);
        adapterResult.setData(result.getList());
        vb.listRecycler.setAdapter(adapterResult);
        vb.listRecycler.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void onInitClick() {
        vb.imBack.setOnClickListener(this);
        vb.layoutError.tvRetry.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();
        } else if (v.equals(vb.layoutError.tvRetry)) {
            vm.getList(key);
        }

    }

    @Override
    public void onClickResultSong(ResponseResultListChild child, int index) {
        String videoId = child.getVideoId();
        String playListId = child.getPlayListId();
        String browserId = child.getBrowserId();
        String pageType = child.getPageType();
        String thumbnail = child.getThumbnail();
        String songName = child.getSongName();
        String subTitle = child.getSubTitle();

        if (videoId != null && !videoId.isEmpty()) {
            ResponseSingle responseSingle = new ResponseSingle();
            responseSingle.setSongTitle(songName);
            responseSingle.setSingerHead(thumbnail);
            responseSingle.setVideoId(videoId);
            responseSingle.setPlaylistId(playListId);
            Intent intent = new Intent(this, PlayActivity.class);
            intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_SINGER, responseSingle);
            intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_CATEGORY_LIST_INDEX, index);
            CommonUtils.LogMsg("-------------index=" + index);
            startActivity(intent);
        } else {
            switch (pageType) {
                case "MUSIC_PAGE_TYPE_ALBUM":
                    //专辑
//                    CommonUtils.LogMsg("------------专辑-index=" + index + "--subTitle=" + subTitle);
                case "MUSIC_PAGE_TYPE_PLAYLIST":
                    //精选
                    Intent intent1 = new Intent(this, CategoryListActivity.class);
                    intent1.putExtra(MyValue.KEY_CATEGORY_LIST_TYPE, pageType);
                    intent1.putExtra(MyValue.KEY_CATEGORY_LIST_SINGER_NAME, subTitle);
                    intent1.putExtra(MyValue.KEY_CATEGORY_LIST_BROWSER_ID, browserId);
                    startActivity(intent1);
                    break;
                case "MUSIC_PAGE_TYPE_ARTIST":
                    //粉丝可能还喜欢
                    Intent intent = new Intent(this, ResultListActivity.class);
                    intent.putExtra(MyValue.KEY_SEARCH_RESULT_BROWSER_ID, browserId);
                    startActivity(intent);
                    finish();
                    break;
            }


        }


    }

}