package com.cooleshow.student.ui.main;

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

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

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.BaseMVPFragment;
import com.cooleshow.student.adapter.HomeMenuItemAdapter;
import com.cooleshow.student.bean.AppHomeBean;
import com.cooleshow.student.databinding.FragmentHomeMenuBinding;
import com.cooleshow.student.presenter.main.HomeMenuPresenter;

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

/**
 * 创建日期：2022/5/26 17:33
 *
 * @author Ryan
 * 类说明：
 */
public class HomeMenuFragment extends BaseMVPFragment<FragmentHomeMenuBinding, HomeMenuPresenter> implements View.OnClickListener {
    @Override
    public void onClick(View view) {

    }

    public static HomeMenuFragment newInstance(List<AppHomeBean.ItemBean> itemBeanList) {
        Bundle args = new Bundle();
        HomeMenuFragment fragment = new HomeMenuFragment();
        args.putSerializable("itemBeanList", (Serializable) itemBeanList);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    protected HomeMenuPresenter createPresenter() {
        return new HomeMenuPresenter();
    }

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

    @Override
    protected void initView(View rootView) {
        itemBeanList = (List<AppHomeBean.ItemBean>) getArguments().getSerializable("itemBeanList");
        RecyclerView rvMenu = mViewBinding.rvMenu;
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 5);
        rvMenu.setLayoutManager(gridLayoutManager);
        HomeMenuItemAdapter addPhotoListAdapter = new HomeMenuItemAdapter(itemBeanList);
        rvMenu.setAdapter(addPhotoListAdapter);
        addPhotoListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                AppHomeBean.ItemBean item = (AppHomeBean.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() {

    }
}
