package com.uiui.zyappstore.activity;

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

import android.view.View;
import android.widget.ImageView;

import com.uiui.zyappstore.R;
import com.uiui.zyappstore.adapter.LocalAppAdapter;
import com.uiui.zyappstore.base.BaseActivity;
import com.uiui.zyappstore.base.RefreshManager;
import com.uiui.zyappstore.bean.LocalApp;
import com.uiui.zyappstore.utils.ApkUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class LocalManagerActivity extends BaseActivity implements RefreshManager.RefreshInterface {
    private RecyclerView mRvApp;
    private SmartRefreshLayout mRefreshLayout;
    private List<LocalApp> localAppList = new ArrayList<>();
    private LocalAppAdapter adapter;
    private ImageView iv_nodata;

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

    @Override
    protected void initView() {
        mRvApp = findViewById(R.id.local_app_rv_app);
        iv_nodata = findViewById(R.id.iv_nodata);
        mRefreshLayout = findViewById(R.id.local_app_refresh_layout);
        RefreshManager.getInstance().register(this);

        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.setEnableRefresh(true);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                initDatas();
            }
        });
        mRefreshLayout.autoRefresh();

        adapter = new LocalAppAdapter(this);
        adapter.setHasStableIds(true);
        mRvApp.setAdapter(adapter);
        mRvApp.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    }

    @Override
    protected void initData() {
        initDatas();
    }

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

    private void initDatas() {
        localAppList = ApkUtils.getLocalApp(LocalManagerActivity.this);
        adapter.setLocalAppList(localAppList);
        adapter.notifyDataSetChanged();
        mRefreshLayout.finishRefresh();
        if (localAppList.size() == 0) {
            mRvApp.setVisibility(View.GONE);
            iv_nodata.setVisibility(View.VISIBLE);
        } else {
            mRvApp.setVisibility(View.VISIBLE);
            iv_nodata.setVisibility(View.GONE);
        }
    }

    @Override
    protected void setListener() {

    }

    @Override
    public void onRefresh() {
        mRefreshLayout.autoRefresh();
    }

    @Override
    public void onLoadBitMap(boolean isLoad) {

    }

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