package com.hainaos.vc.fragment.pic;

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

import androidx.fragment.app.Fragment;
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.fragment.BaseMvvmFragment;
import com.hainaos.vc.bean.PhotoInfo;
import com.hainaos.vc.databinding.FragmentPictureBinding;
import com.hainaos.vc.utils.ScreenUtils;
import com.hainaos.vc.utils.ToastUtil;
import com.hainaos.vc.view.EquallyDividedItemDecoration;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PictureFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PictureFragment extends BaseMvvmFragment<PictureViewModel, FragmentPictureBinding> {
    private static final String TAG = "PictureFragment";

    private Activity mContext;

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

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public PictureFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PictureFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PictureFragment newInstance(String param1, String param2) {
        PictureFragment fragment = new PictureFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

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

    @Override
    protected void initView(Bundle bundle) {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, SPAN_COUNT, LinearLayoutManager.VERTICAL, false);
        mViewDataBinding.recyclerView.setLayoutManager(layoutManager);

        EquallyDividedItemDecoration equallyDividedItemDecoration = new EquallyDividedItemDecoration(SPAN_COUNT, ScreenUtils.dip2px(mContext, 1));
        mViewDataBinding.recyclerView.addItemDecoration(equallyDividedItemDecoration);

        mPicAdapter = new PicAdapter(layoutManager);
        mViewDataBinding.recyclerView.setAdapter(mPicAdapter);
        mViewDataBinding.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mViewModel.getHomePhoto();
                mViewDataBinding.swipeRefreshLayout.setRefreshing(true);
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        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);
            }
        });
        mViewModel.getHomePhoto();
        mViewDataBinding.swipeRefreshLayout.setRefreshing(true);
    }

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

    }

    public class BtnClick {

    }

}
