package com.xwkj.jcjcgz.mvp.ui.activity;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;

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

import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xwkj.jcjcgz.R;
import com.xwkj.jcjcgz.di.component.DaggerAppListComponent;
import com.xwkj.jcjcgz.mvp.contract.AppListContract;
import com.xwkj.jcjcgz.mvp.model.entity.AppInfo;
import com.xwkj.jcjcgz.mvp.presenter.AppListPresenter;
import com.xwkj.jcjcgz.mvp.ui.adapter.AppListAdapter;
import com.yaoxiaowen.download.DownloadHelper;
import com.zkjc.common.base.ZkjcBaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/04/2024 16:46
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class AppListActivity extends ZkjcBaseActivity<AppListPresenter> implements AppListContract.View {

    @BindView(R.id.app_list_rv)
    RecyclerView recyclerView;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerAppListComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_app_list; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
//        DownloadHelper helper = DownloadHelper.getInstance();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        PackageManager manager = getPackageManager();
        List<ApplicationInfo> infoList = manager.getInstalledApplications(PackageManager.GET_META_DATA);
        List<AppInfo> appInfoList = new ArrayList<>();
        for (ApplicationInfo info: infoList) {
            AppInfo appInfo = new AppInfo();
            appInfo.setName(manager.getApplicationLabel(info).toString());
            appInfoList.add(appInfo);
        }
        AppListAdapter adapter = new AppListAdapter(appInfoList);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    public void getAppList(View view) {

        Map<String, String> map = new HashMap<>();
        map.put("current", "1");
        map.put("size", "10");
        mPresenter.getAppList(map);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onGetAppListSuccess(String result) {

    }
}
