package com.swiftcleaner.chovey.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.model.bean.AppInfo;
import com.swiftcleaner.chovey.model.bean.AppInfoBean;

import java.util.List;


public class AppProcessAdapter extends RecyclerView.Adapter<AppProcessAdapter.AppInfoViewHolder> {
    private List<AppInfoBean> appList;
    Context context;

    public AppProcessAdapter(List<AppInfoBean> appList, Context context) {
        this.appList = appList;
        this.context = context;
    }

    @NonNull
    @Override
    public AppInfoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app_process_list, parent, false);
        return new AppInfoViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull AppInfoViewHolder holder, int position) {
        AppInfoBean appInfo = appList.get(position);
        holder.button_delete.setOnClickListener(v->{
            // 打开应用详情页（进程）
            Intent intentAppDetails = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intentAppDetails.setData(Uri.parse("package:" + appInfo.getPackageName()));
            context.startActivity(intentAppDetails);
        });
        Glide.with(context)
                .load(appInfo.getAppIcon())
                .apply(new RequestOptions().transform(new RoundedCorners(10)))
                .error(R.mipmap.ic_launcher)
                .placeholder(R.mipmap.ic_launcher)
                .into(holder.icon);
        holder.appName.setText(appInfo.getAppName());
    }


    @Override
    public int getItemCount() {
        return appList.size();
    }

    public static class AppInfoViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView appName;
        AppCompatButton button_delete;

        public AppInfoViewHolder(@NonNull View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.item_battery_img);
            appName = itemView.findViewById(R.id.item_battery_name);
            button_delete = itemView.findViewById(R.id.item_button_battery_delete);
        }
    }
}
