package com.uiui.zyappstore.fragment;

import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.uiui.zyappstore.R;
import com.uiui.zyappstore.adapter.KindAdapter;
import com.uiui.zyappstore.base.BaseFragment;
import com.uiui.zyappstore.base.RefreshManager;
import com.uiui.zyappstore.bean.Kind;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by asus on 2017/10/23.
 * 分类的页面
 */

public class KindFragment extends BaseFragment implements RefreshManager.RefreshInterface {
    private RecyclerView mRvKind;

    private KindAdapter adapter;
    private List<Kind> kindList;
    private SmartRefreshLayout mRefreshLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_kind, container, false);
        mRvKind = view.findViewById(R.id.kind_rv_kind);
        mRefreshLayout = view.findViewById(R.id.featured_refresh_layout);
        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                initData();
            }
        });
        initView();
        initData();
        return view;
    }

    private void initView() {
        kindList = new ArrayList<>();
        kindList.add(new Kind("语文", 1, R.drawable.icon_language1, 1));
        kindList.add(new Kind("数学", 2, R.drawable.icon_math1, 1));
        kindList.add(new Kind("英语", 3, R.drawable.icon_english1, 1));
        kindList.add(new Kind("物理", 4, R.drawable.icon_physics1, 1));
        kindList.add(new Kind("化学", 5, R.drawable.icon_chemistry1, 1));
        kindList.add(new Kind("生物", 6, R.drawable.icon_biology1, 1));
        kindList.add(new Kind("政治", 7, R.drawable.icon_politics1, 1));
        kindList.add(new Kind("历史", 8, R.drawable.icon_history1, 1));
        kindList.add(new Kind("地理", 9, R.drawable.icon_geography1, 1));

        kindList.add(new Kind("娱乐", 0, R.drawable.icon_game1, 0));

        adapter = new KindAdapter(kindList, getContext());
        mRvKind.setAdapter(adapter);
        mRvKind.setLayoutManager(new LinearLayoutManager(getActivity()));

    }

    private void initData() {
//        OKGO.getSubClassfy(mHandler);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 200:
                    List<String> list = (List<String>) msg.obj;
                    showAdapter(list);
                    break;
                case -200:
                    hideAdapter();
                    break;
            }
            mRefreshLayout.finishRefresh();
        }
    };

    private void showAdapter(List<String> list) {
        if (null != list && list.size() != 0) {
            for (int i = 0; i < kindList.size() - 1; i++) {
                if (list.contains(kindList.get(i).getName())) {
                    kindList.get(i).setHide(0);
                } else {
                    kindList.get(i).setHide(1);
                }
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void hideAdapter() {
        for (int i = 0; i < kindList.size() - 1; i++) {
            kindList.get(i).setHide(1);
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onRefresh() {
        mRefreshLayout.autoRefresh();
    }

    @Override
    public void onLoadBitMap(boolean isLoad) {

    }
}
