package com.ttstd.dialer.fragment.app;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

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

import com.ttstd.dialer.R;
import com.ttstd.dialer.adapter.AppAdapter;
import com.ttstd.dialer.base.mvvm.fragment.BaseMvvmFragment;
import com.ttstd.dialer.databinding.FragmentAppBinding;
import com.ttstd.dialer.db.app.AppInfo;
import com.ttstd.dialer.view.EqualHeightDecoration;

import java.io.Serializable;
import java.util.List;

public class AppFragment extends BaseMvvmFragment<AppViewModel, FragmentAppBinding> {
    private static final String TAG = "AppFragment";

    private static final String ARG_APP_LIST = "app_list";

    private Activity mContext;

    private AppAdapter mAppAdapter;
    private List<AppInfo> mAppInfos;

//    public static AppFragment newInstance(List<AppInfo> apkList) {
//        AppFragment appFragment = new AppFragment();
//        Log.e(TAG, "newInstance: " + appFragment);
//        Bundle args = new Bundle();
//        args.putSerializable(ARG_APP_LIST, (Serializable) apkList);
//        appFragment.setArguments(args);
//        return appFragment;
//    }


    public AppFragment(List<AppInfo> appInfos) {
        mAppInfos = appInfos;
        Log.e(TAG, "AppFragment: mAppInfos = " + mAppInfos.hashCode());
    }

    public interface UpdateCallback {
        void onUpdate();
    }

    private UpdateCallback mUpdateCallback;

    public void setUpdateCallback(UpdateCallback updateCallback) {
        mUpdateCallback = updateCallback;
    }

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

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

    @Override
    protected void initView(Bundle bundle) {
        Log.e(TAG, "initView: " + this.hashCode());

        mAppAdapter = new AppAdapter(LoaderManager.getInstance(this));
        mAppAdapter.setShortcutCallback(new AppAdapter.ShortcutCallback() {
            @Override
            public void setAppInside(AppInfo appInfo) {
                appInfo.setOutside(0);
                mViewModel.updateAppInfo(appInfo);
            }
        });
        mViewDataBinding.recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        mViewDataBinding.recyclerView.addItemDecoration(new EqualHeightDecoration(3));
        mViewDataBinding.recyclerView.setAdapter(mAppAdapter);

        if (mAppInfos != null) {
            Log.e(TAG, "initView: mAppInfos size = " + mAppInfos.size());
            Log.e(TAG, "initView: mAppInfos = " + mAppInfos.hashCode());
            mAppAdapter.setAppInfos(mAppInfos);
        }

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        Log.e(TAG, "initData: ");
        Log.e(TAG, "initData: " + mViewModel.mAppUpdateData);
        mViewModel.mAppUpdateData.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if (integer > 0) {

                }
                if (mUpdateCallback != null) {
                    mUpdateCallback.onUpdate();
                }
            }
        });
//        if (getArguments() != null) {
//            mAppInfos = (List<AppInfo>) getArguments().getSerializable(ARG_APP_LIST);
//        }

    }

    @Override
    public void fetchData() {
        Log.e(TAG, "fetchData: ");

    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: ");
        if (mAppInfos != null) {
            mAppAdapter.setAppInfos(mAppInfos);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.e(TAG, "onDestroyView: ");
    }

    public class BtnClick {

    }
}
