package com.keyboard.theme.app.keyboard.ui;

import android.content.Intent;

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

import com.keyboard.theme.app.keyboard.MyAppllication;
import com.keyboard.theme.app.keyboard.adapter.HomeClassAdapter;
import com.keyboard.theme.app.keyboard.api.OnHomeApi;
import com.keyboard.theme.app.keyboard.data.Info;
import com.keyboard.theme.app.keyboard.data.InfoChild;
import com.keyboard.theme.app.keyboard.databinding.ActivityHomeBinding;
import com.keyboard.theme.app.keyboard.utils.Common;
import com.keyboard.theme.app.keyboard.utils.StaticValue;

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

public class MainActivity extends BaseActivity<ActivityHomeBinding> implements OnHomeApi {

    private HomeClassAdapter homeClassAdapter;


    private int index = 0;

    private ActivityHomeBinding VB;

    @NonNull
    @Override
    public ActivityHomeBinding returnViewbinding() {
        Common.INSTANCE.initFull(this, false);
        return ActivityHomeBinding.inflate(getLayoutInflater());
    }

    @Override
    public void onInit() {
        VB = getVb();
        Common.INSTANCE.setFont(VB.title);
        initAdapter();

    }


    private void initAdapter() {

        List<Info> tempList = new ArrayList<>();
        List<Info> data = MyAppllication.Companion.getData();
        for (Info datum : data) {
            Info info = new Info();
            List<InfoChild> list = datum.getList();
            info.setClassName(datum.getClassName());
            Collections.shuffle(list);
            List<InfoChild> infoChildren = list.subList(0, 4);
            info.setList(infoChildren);
            tempList.add(info);
        }
        homeClassAdapter = new HomeClassAdapter(this, tempList);

        LinearLayoutManager linearLayoutManager =
                new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        VB.categoryRecycler.setLayoutManager(linearLayoutManager);
        VB.categoryRecycler.setAdapter(homeClassAdapter);
        homeClassAdapter.setListener(this);
    }


    @Override
    public void onHomeClick(InfoChild infoChild) {
        Intent setIntent = new Intent(this, ActivityApply.class);
        setIntent.putExtra(StaticValue.KEYTheme, infoChild);
        startActivity(setIntent);
    }

    @Override
    public void onHomeClickMore(Info info) {
        Intent setIntent = new Intent(this, ClassActivity.class);
        setIntent.putExtra(ClassActivity.KEY,info.getClassName());
        startActivity(setIntent);
    }
}
