package com.ttstd.dialer.activity.app;

import androidx.lifecycle.Observer;
import androidx.loader.app.LoaderManager;
import androidx.recyclerview.widget.GridLayoutManager;

import com.tencent.mmkv.MMKV;
import com.ttstd.dialer.R;
import com.ttstd.dialer.adapter.MoreAppAdapter;
import com.ttstd.dialer.base.mvvm.BaseMvvmActivity;
import com.ttstd.dialer.config.CommonConfig;
import com.ttstd.dialer.databinding.ActivityAppListBinding;
import com.ttstd.dialer.db.app.AppInfo;

import java.util.List;

public class AppListActivity extends BaseMvvmActivity<AppListViewModel, ActivityAppListBinding> {
    private static final String TAG = "AppListActivity";

    private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);

    private MoreAppAdapter mMoreAppAdapter;


    @Override
    public boolean setNightMode() {
        return true;
    }

    @Override
    public boolean setfitWindow() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_app_list;
    }

    @Override
    protected void initDataBinding() {
        mViewModel.setContext(this);
        mViewModel.setVDBinding(mViewDataBinding);
        mViewModel.setLifecycle(getLifecycleSubject());
        mViewDataBinding.setClick(new BtnClick());
    }

    @Override
    protected void initView() {
        mMoreAppAdapter = new MoreAppAdapter(LoaderManager.getInstance(this));
        mMoreAppAdapter.setShortcutCallback(new MoreAppAdapter.ShortcutCallback() {
            @Override
            public void setAppOutside(AppInfo appInfo) {
                appInfo.setOutside(1);
                mViewModel.updateAppInfo(appInfo);
            }
        });
        mViewDataBinding.recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mViewDataBinding.recyclerView.setAdapter(mMoreAppAdapter);

    }

    @Override
    protected void initData() {
        mViewModel.mDesktopSortAppData.observe(this, new Observer<List<AppInfo>>() {
            @Override
            public void onChanged(List<AppInfo> appInfos) {
                mMoreAppAdapter.setAppInfos(appInfos);
            }
        });

        mViewModel.getDbAppList();

        mViewModel.mAppUpdateData.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if (integer > 0) {

                }
                mViewModel.getDbAppList();
            }
        });
    }


    public class BtnClick {

    }
}
