package com.uiui.zyappstore.adapter;

import android.content.Context;
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.UpdateAppInfo;

import java.util.List;

public class UpdateAppAdapter extends RecyclerView.Adapter<UpdateAppAdapter.UpdateAppViewHolder> {
    private Context context;

    private List<UpdateAppInfo> localAppList;

    public UpdateAppAdapter(List<UpdateAppInfo> localAppList, Context context) {
        this.context = context;
        this.localAppList = localAppList;

    }

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

    @Override
    public void onBindViewHolder(UpdateAppViewHolder holder, int position) {
        final UpdateAppInfo appInfo = localAppList.get(position);
//        final DownloadTask downloadTask = OkDownload.getInstance().getTask(appInfo.getURL());
//        holder.setTag(appInfo.getURL());
//        if (downloadTask != null) {
//            downloadTask.register(new ListDownloadListener(appInfo.getURL(), holder));
//            holder.setTask(downloadTask);
//        holder.bind();
//            holder.refresh(downloadTask.progress);
//        }

        holder.ivIcon.setImageDrawable(appInfo.getIcon());
        holder.tvName.setText(appInfo.getAppName());
        holder.tvVersion.setText(appInfo.getVersionName() + "->" + appInfo.getNewVersionName());
        holder.btnDownLoad.setText("更新");
        holder.btnDownLoad.setTextColor(context.getColor(R.color.red));
        holder.btnDownLoad.setBackground(context.getDrawable(R.drawable.btn_shap_open));
        holder.btnDownLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                downloadApk(appInfo);
            }
        });


    }

//    private void downloadApk(UpdateAppInfo appInfo) {
//        GetRequest<File> request = OkGo.get(appInfo.getURL());
//        //这里第一个参数是tag，代表下载任务的唯一标识，传任意字符串都行，需要保证唯一,我这里用url作为了tag
//        OkDownload.request(appInfo.getURL(), request)//
////                            .priority(apk.priority)//
//                .extra1(appInfo.getAppInfo())//
//                .save()//
//                .register(new LogDownloadListener())//
//                .start();
//        notifyDataSetChanged();
//    }

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

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

    public static class UpdateAppViewHolder extends RecyclerView.ViewHolder {
        ImageView ivIcon;
        TextView tvName, tvVersion;
        TextView tvUpdate;
        Button btnDownLoad;

//        private DownloadTask task;
        private String tag;

        public UpdateAppViewHolder(View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.local_app_iv_icon);
            tvName = itemView.findViewById(R.id.local_app_tv_name);
            tvVersion = itemView.findViewById(R.id.local_app_version);
            tvUpdate = itemView.findViewById(R.id.local_app_update);
            btnDownLoad = itemView.findViewById(R.id.local_app_btn_delete);
        }

        public void bind() {

        }

//        void refresh(Progress progress) {
//            switch (progress.status) {
//                case Progress.NONE:
//                    btnDownLoad.setText("下载");
//                    break;
//                case Progress.PAUSE:
//                    btnDownLoad.setText("继续");
//                    break;
//                case Progress.ERROR:
//                    btnDownLoad.setText("出错");
//                    break;
//                case Progress.WAITING:
//                    btnDownLoad.setText("等待");
//                    break;
//                case Progress.FINISH:
//                    btnDownLoad.setText("完成");
//
//                    break;
//                case Progress.LOADING:
//                    btnDownLoad.setText((int) (progress.fraction * 100) + "%");
//                    break;
//            }
//
//        }
//
//        void setTask(DownloadTask task) {
//            this.task = task;
//        }
//
//        void setTag(String tag) {
//            this.tag = tag;
//        }
//
//        String getTag() {
//            return tag;
//        }
    }

//    private class ListDownloadListener extends DownloadListener {
//
//        private UpdateAppViewHolder holder;
//
//        ListDownloadListener(Object tag, UpdateAppViewHolder holder) {
//            super(tag);
//            this.holder = holder;
//        }
//
//        @Override
//        public void onStart(Progress progress) {
//
//        }
//
//        @Override
//        public void onProgress(Progress progress) {
//            if (tag == holder.getTag()) {
//                holder.refresh(progress);
//            }
//        }
//
//        @Override
//        public void onError(Progress progress) {
//            Throwable throwable = progress.exception;
//            if (throwable != null) throwable.printStackTrace();
//            ToastUtil.show(((AppInfo) progress.extra1).getApp_name() + "\t下载失败，请重试");
//        }
//
//        @Override
//        public void onFinish(final File file, Progress progress) {
//            ToastUtil.show(((AppInfo) progress.extra1).getApp_name() + "\t下载完成");
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    ApkUtils.installApp(context,file.getAbsolutePath() );
//                }
//            }).start();
//        }
//
//        @Override
//        public void onRemove(Progress progress) {
//        }
//    }

}
