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

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.OptIn;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.media3.common.util.UnstableApi;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;

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.BitmapTransitionOptions;
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.adapter.AdapterCategoryList;
import com.hi.music.player.api.HomeItemClickListener;
import com.hi.music.player.api.onImageColorListener;
import com.hi.music.player.databinding.ActivityCategoryListBinding;
import com.hi.music.player.databinding.ActivityHomeBinding;
import com.hi.music.player.helper.CommonUtils;
import com.hi.music.player.helper.MyValue;
import com.hi.music.player.javabean.response.ResponseCategoryList;
import com.hi.music.player.javabean.response.ResponsePlayListInfo;
import com.hi.music.player.javabean.response.child.ResponseCategory;
import com.hi.music.player.javabean.response.child.ResponseSingle;
import com.hi.music.player.ui.activity.viewmodel.VMApplication;
import com.hi.music.player.ui.fragmnt.viewmodel.VMCategoryList;
import com.hi.music.player.ui.fragmnt.viewmodel.VMHome;

import java.io.Serializable;

import jp.wasabeef.glide.transformations.BlurTransformation;


/**
 * 音乐合集或者专辑列表页面
 */
public class CategoryListActivity extends BaseActivity<ActivityCategoryListBinding> implements HomeItemClickListener {


    private VMCategoryList vm;
//    private VMApplication vmApplication;

    private ResponseCategoryList mCategoryList;
    private String mPageType;
    private String twoSubtitle;

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

    @Override
    protected void onCreateInit() {
        vm = getActivityScopeViewModel(VMCategoryList.class);
//        vmApplication = getApplicationScopeViewModel(VMApplication.class);
        Intent intent = getIntent();
        if (intent == null) {
            return;
        }

        if (!intent.hasExtra(MyValue.KEY_PLAY_ACTIVITY_CATEGORY)) {
            return;
        }
        ResponseCategory category = (ResponseCategory) intent.getSerializableExtra(MyValue.KEY_PLAY_ACTIVITY_CATEGORY);
        if (category == null)
            return;
        mPageType = category.getPageType();
        twoSubtitle = category.getTwoSubtitle();


        vb.pbLoading.setVisibility(View.VISIBLE);
        vm.getList(category.getBrowseId());
        vm.data.observe(this, new Observer<ResponseCategoryList>() {
            @Override
            public void onChanged(ResponseCategoryList responseCategoryList) {
                if (responseCategoryList == null) {

                    return;
                }
                mCategoryList = responseCategoryList;
                loadInfo(mCategoryList);

            }
        });
    }

    @Override
    protected void onInitClick() {
        vb.imBack.setOnClickListener(this);
        vb.btnPlay.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.btnPlay)) {
            int musicIndex = 0;
            Intent intent = new Intent(this, PlayActivity.class);
            intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_CATEGORY_LIST, mCategoryList.getList().get(musicIndex));
//            intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_CATEGORY_LIST_INDEX,musicIndex);
            intent.putExtra(MyValue.KEY_ENTER_SOURCE, MyValue.TYPE_ENTER_SOURCE_CATEGORY);
            startActivity(intent);
            vmApplication.reSetPlayList(mCategoryList.getList());
        }
    }

    private void loadInfo(ResponseCategoryList info) {
        vb.btnPlay.setVisibility(View.VISIBLE);
        switch (mPageType) {
            case MyValue.PAGE_TYPE_LIST:
                vb.tvSingerName.setVisibility(View.GONE);
                break;
            case MyValue.PAGE_TYPE_ALBUM:
                vb.tvSingerName.setVisibility(View.VISIBLE);
                String[] split = twoSubtitle.split("•");
                CommonUtils.LogMsg("twoSubtitle=" + twoSubtitle);
                if (split != null && split.length > 1) {
                    vb.tvSingerName.setText(split[1]);
                    for (ResponsePlayListInfo playListInfo : info.getList()) {
                        playListInfo.setSingerName(split[1]);
                    }

                }
                break;
        }
        vb.tvTitle.setText(info.getTitle());
        vb.tvSubTitle.setText(info.getDescription());

        AdapterCategoryList adapterCategoryList = new AdapterCategoryList();
        adapterCategoryList.setHomeItemClickListener(this);
        adapterCategoryList.setPageType(mPageType);
        vb.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapterCategoryList.setData(info.getList());
        vb.recyclerview.setAdapter(adapterCategoryList);
        loadCovert(info.getCovert());

    }


    private void loadCovert(String url) {
        // 加载图片并应用高斯模糊效果
        Glide.with(this)
                .load(url)
                .apply(bitmapTransform(new BlurTransformation(25, 3))) // 设置模糊半径和模糊采样
                .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) {

//                        CommonUtils.extractColorsFromImage(resource,vb.imBg);
                        return false;
                    }
                })
                .preload();


        Glide.with(MusicApplication.myApplication)
                .asDrawable()
                .apply(bitmapTransform(new RoundedCorners(CommonUtils.dpToPx(10))))
                .load(url)
                .placeholder(R.drawable.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());
                        vb.pbLoading.setVisibility(View.GONE);
                        return false;
                    }

                    @OptIn(markerClass = UnstableApi.class)
                    @Override
                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
//                        CommonUtils.getDominantDarkColor1(resource, new onImageColorListener() {
//                            @Override
//                            public void onImageColor(int color) {
//                                if (color == -1) {
//                                    return;
//                                }
//
//                            }
//                        });
                        vb.pbLoading.setVisibility(View.GONE);
                        return false;
                    }
                })
                .into(vb.imCovert);


    }



    @Override
    public void onClickItemCategoryList(ResponsePlayListInfo data, int musicIndex) {
        Intent intent = new Intent(this, PlayActivity.class);
        intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_CATEGORY_LIST, data);
        intent.putExtra(MyValue.KEY_PLAY_ACTIVITY_CATEGORY_LIST_INDEX, musicIndex);
        intent.putExtra(MyValue.KEY_ENTER_SOURCE, MyValue.TYPE_ENTER_SOURCE_CATEGORY);
        startActivity(intent);
        vmApplication.reSetPlayList(mCategoryList.getList());
    }

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


    }
}