package com.hainaos.vc.activity.pic;

import android.view.View;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.hainaos.vc.R;
import com.hainaos.vc.adapter.PicAdapter;
import com.hainaos.vc.base.mvvm.BaseMvvmActivity;
import com.hainaos.vc.bean.PhotoInfo;
import com.hainaos.vc.databinding.ActivityGalleryBinding;
import com.hainaos.vc.utils.ToastUtil;
import com.hainaos.vc.view.BGAGridDivider;

import java.util.ArrayList;

public class GalleryActivity extends BaseMvvmActivity<GalleryViewModel, ActivityGalleryBinding> {

    private PicAdapter mPicAdapter;
    private static final int SPAN_COUNT = 3;

    @Override
    public int getLayoutId() {
        return R.layout.activity_gallery;
    }

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

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

    @Override
    public void initView() {
        GridLayoutManager layoutManager = new GridLayoutManager(this, SPAN_COUNT, LinearLayoutManager.VERTICAL, false);
        mPicAdapter = new PicAdapter(layoutManager);
        mViewDataBinding.recyclerView.setLayoutManager(layoutManager);
        mViewDataBinding.recyclerView.addItemDecoration(BGAGridDivider.newInstanceWithSpaceRes(R.dimen.dp_2));
        mViewDataBinding.recyclerView.setAdapter(mPicAdapter);
        mViewDataBinding.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getHomePhoto();
            }
        });
    }

    @Override
    public void initData() {
        mViewModel.mPhotoInfosData.observe(this, new Observer<ArrayList<PhotoInfo>>() {
            @Override
            public void onChanged(ArrayList<PhotoInfo> photoInfos) {
                mPicAdapter.setPhotoInfos(photoInfos);
                if (photoInfos == null || photoInfos.size() == 0) {
                    mViewDataBinding.clNodata.setVisibility(View.VISIBLE);
                    mViewDataBinding.recyclerView.setVisibility(View.GONE);
                    ToastUtil.show("没有数据");
                } else {
                    mViewDataBinding.clNodata.setVisibility(View.GONE);
                    mViewDataBinding.recyclerView.setVisibility(View.VISIBLE);
                }
            }
        });
        mViewModel.mCompleteData.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                mViewDataBinding.swipeRefreshLayout.setRefreshing(false);
            }
        });
        getHomePhoto();
    }

    private void getHomePhoto() {
        mViewDataBinding.swipeRefreshLayout.setRefreshing(true);
        mViewModel.getHomePhoto();
    }

    public class BtnClick {
        public void exit(View view) {
            finish();
        }

        public void refresh(View view) {
            getHomePhoto();
        }
    }
}
