package com.cooleshow.teacher.ui.main;

import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.fragment.BaseFragment;
import com.cooleshow.teacher.adapter.HomeMenuItemAdapter;
import com.cooleshow.teacher.bean.HomeMenuBean;
import com.cooleshow.teacher.databinding.FragmentHomeMenuBinding;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 创建日期：2022/5/26 17:33
 *
 * @author Ryan
 * 类说明：
 */
public class HomeMenuFragment extends BaseFragment<FragmentHomeMenuBinding> {

    public static HomeMenuFragment newInstance(ArrayList<HomeMenuBean.ItemBean> itemBeanList) {
        Bundle args = new Bundle();
        HomeMenuFragment fragment = new HomeMenuFragment();
        args.putParcelableArrayList("itemBeanList", itemBeanList);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected FragmentHomeMenuBinding getLayoutView() {
        return FragmentHomeMenuBinding.inflate(getLayoutInflater());
    }


    private ArrayList<HomeMenuBean.ItemBean> itemBeanList = new ArrayList<>();

    @Override
    protected void initView(View rootView) {
        if (getArguments() != null) {
            itemBeanList = getArguments().getParcelableArrayList("itemBeanList");
        }
        RecyclerView rvMenu = mViewBinding.rvMenu;
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        rvMenu.setLayoutManager(gridLayoutManager);
        HomeMenuItemAdapter addPhotoListAdapter = new HomeMenuItemAdapter();
        rvMenu.setAdapter(addPhotoListAdapter);
        addPhotoListAdapter.setNewInstance(itemBeanList);
        addPhotoListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (position < addPhotoListAdapter.getData().size()) {
                    HomeMenuBean.ItemBean item = (HomeMenuBean.ItemBean) adapter.getItem(position);
                    if (!TextUtils.isEmpty(item.linkUrl)) {
                        ARouter.getInstance()
                                .build(RouterPath.WebCenter.ACTIVITY_HTML)
                                .withString(WebConstants.WEB_URL, item.linkUrl)
                                .navigation();
                    }
                }
            }
        });
    }

    @Override
    protected void initData() {

    }
}
