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 AppInfoAdapter extends RecyclerView.Adapter<AppInfoAdapter.AppInfoViewHolder> {
    private List<AppInfoBean> appList;
    Context context;

    public AppInfoAdapter(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_battery_list, parent, false);
        return new AppInfoViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull AppInfoViewHolder holder, int position) {
        AppInfoBean appInfoBean = appList.get(position);
        holder.button_delete.setOnClickListener(v->{
            // 卸载应用
            Intent intentUninstall = new Intent(Intent.ACTION_DELETE);
            intentUninstall.setData(Uri.parse("package:" + appInfoBean.getPackageName()));
            context.startActivity(intentUninstall);
            notifyDataSetChanged();
        });
        Glide.with(context)
                .load(appInfoBean.getAppIcon())
                .apply(new RequestOptions().transform(new RoundedCorners(10)))
                .error(R.mipmap.ic_launcher)
                .placeholder(R.mipmap.ic_launcher)
                .into(holder.icon);
        holder.appName.setText(appInfoBean.getAppName());
//        holder.memoryUsage.setText(String.format("", formatMemoryUsage(appInfoBean.)));
    }
    @Override
    public int getItemCount() {
        return appList.size();
    }

    public static class AppInfoViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView appName;
        TextView memoryUsage;
        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);
            memoryUsage = itemView.findViewById(R.id.item_battery_memory_number);
            button_delete = itemView.findViewById(R.id.item_button_battery_delete);
        }
    }
}
