package com.uiui.zyappstore.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.uiui.zyappstore.R;
import com.uiui.zyappstore.bean.GlideCircleWithBorder;
import com.uiui.zyappstore.bean.LocalApp;
import com.uiui.zyappstore.utils.ApkUtils;
import com.uiui.zyappstore.utils.TimeUtils;
import com.uiui.zyappstore.utils.Utils;
import com.bumptech.glide.Glide;

import java.util.List;


/**
 * Created by asus on 2017/10/26.
 */

public class LocalAppAdapter extends RecyclerView.Adapter<LocalAppAdapter.LocalAppViewHolder> {

    private Context context;

    private List<LocalApp> localAppList;

    public LocalAppAdapter(Context context) {
        this.context = context;
    }

    public void setLocalAppList(List<LocalApp> appList) {
        this.localAppList = appList;
    }

    @Override
    public LocalAppViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LocalAppViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_local_app, parent, false));
    }

    @Override
    public void onBindViewHolder(LocalAppViewHolder holder, int position) {
        final LocalApp localApp = localAppList.get(position);
        Bitmap bitmap = Utils.getRoundedBitmap(Utils.DrawableToBitmap(localApp.getIcon()), context);
        Glide.with(context).load(bitmap)
                .transform(new GlideCircleWithBorder(context))
                .into(holder.ivIcon);
        holder.ivIcon.setImageDrawable(new BitmapDrawable(context.getResources(), bitmap));
        holder.tvName.setText(localApp.getAppName());
        holder.tvVersion.setText("版本:"+localApp.getVersion());
        holder.tvInstallTime.setText("安装时间:"+TimeUtils.transferLongToDate(localApp.getFirstInstallTime()));
        if (localApp.isCanUnintall()) {
            holder.btnDownLoad.setVisibility(View.VISIBLE);
        } else {
            holder.btnDownLoad.setVisibility(View.INVISIBLE);
        }
        holder.btnDownLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ApkUtils.Uninstall(context, localApp.getPackageName());
            }
        });
        holder.btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ApkUtils.openApp(context, localApp.getPackageName());
            }
        });
        String totalLength = Formatter.formatFileSize(context, localApp.getSize());
        holder.tvCompany.setText("大小:"+totalLength);

    }

    @Override
    public int getItemCount() {
        return localAppList == null ? 0 : localAppList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public static class LocalAppViewHolder extends RecyclerView.ViewHolder {
        ImageView ivIcon;
        TextView tvCompany, tvName, tvVersion, tvInstallTime;
        TextView tvUpdate;
        Button btnDownLoad, btnOpen;

        public LocalAppViewHolder(View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.local_app_iv_icon);
            tvCompany = itemView.findViewById(R.id.local_app_tv_company);
            tvName = itemView.findViewById(R.id.local_app_tv_name);
            tvVersion = itemView.findViewById(R.id.local_app_version);
            tvInstallTime = itemView.findViewById(R.id.local_app_install_time);
            tvUpdate = itemView.findViewById(R.id.local_app_update);
            btnDownLoad = itemView.findViewById(R.id.local_app_btn_delete);
            btnOpen = itemView.findViewById(R.id.local_app_btn_open);
        }
    }
}
