package com.uiui.zyappstore.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.uiui.zyappstore.R;
import com.uiui.zyappstore.adapter.DownloadManagerAdapter;
import com.uiui.zyappstore.base.BaseActivity;

public class DownloadManagerActivity extends BaseActivity /*implements XExecutor.OnAllTaskEndListener*/ {
    private RecyclerView recyclerView;
    private DownloadManagerAdapter adapter;
//    private OkDownload okDownload;
    private ImageView iv_notask;

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_download_manager;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recyclerView);
        iv_notask = findViewById(R.id.iv_notask);

    }

    @Override
    protected void initData() {
//        okDownload = OkDownload.getInstance();
        adapter = new DownloadManagerAdapter(this, handler);
//        adapter.updateData(DownloadManagerAdapter.TYPE_ING);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        adapter.updateNotCompleteTask();
        if (adapter.getItemCount() == 0) {
            iv_notask.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            iv_notask.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
//        okDownload.addOnAllTaskEndListener(this);
    }

    @Override
    protected void setListener() {

    }


//    @Override
//    public void onAllTaskEnd() {
//        ToastUtil.show("所有任务已完成");
//        adapter.notifyDataSetChanged();
//
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        okDownload.removeOnAllTaskEndListener(this);
//        adapter.unRegister();
    }

    @Override
    protected void onResume() {
        super.onResume();
        adapter.notifyDataSetChanged();
    }

    public void finish(View view) {
        this.finish();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100) {
//                adapter.updateData(DownloadManagerAdapter.TYPE_ING);
                adapter.notifyDataSetChanged();
                if (adapter.getItemCount() == 0) {
                    iv_notask.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                } else {
                    iv_notask.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                }
            }
        }
    };


}
