package com.photo.keyboard.themes.pager;

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

import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.photo.keyboard.themes.App;
import com.photo.keyboard.themes.adapter.ClassAdapter;
import com.photo.keyboard.themes.databinding.ActivityClassBinding;
import com.photo.keyboard.themes.listener.MainListener;
import com.photo.keyboard.themes.data.Parent;
import com.photo.keyboard.themes.data.ThemeData;
import com.photo.keyboard.themes.manager.Utils;
import com.photo.keyboard.themes.manager.ItemSpace;
import com.photo.keyboard.themes.manager.ConstValue;
import com.photo.keyboard.themes.topon.AdManager;
import com.photo.keyboard.themes.topon.onActionListener;

import java.util.List;
import java.util.Objects;

public class ClassActivity extends BaseActivity<ActivityClassBinding> implements MainListener {

    public static final String KEY = "KEY_class_name";
    private ActivityClassBinding binding;
    private Parent classParent;


    @NonNull
    @Override
    public ActivityClassBinding returnViewbinding() {
        Utils.INSTANCE.initFull(this, false);

        return ActivityClassBinding.inflate(getLayoutInflater());
    }

    @Override
    public void onInit() {
        AdManager.loadAllAd();
        binding = getVb();
        String className = getIntent().getStringExtra(KEY);

        Utils.INSTANCE.setFont(binding.classTitle);
        binding.classTitle.setText(className);

        AdManager.showTopOn(ClassActivity.this, new onActionListener() {
            @Override
            public void onAction() {

            }
        });

        for (Parent datum : App.Companion.getData()) {
            if(Objects.equals(datum.getClassName(), className)){
                classParent = datum;
                break;
            }
        }

        binding.imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initList();

    }

    private void initList(){
        List<ThemeData> list = classParent.getList();
        if(list == null){
            return;
        }
        ClassAdapter ClassAdapter = new ClassAdapter(this, list);
        ClassAdapter.setListener(this);
        ItemSpace itemSpace = new ItemSpace(Utils.INSTANCE.dpToUnitPx(8), Utils.INSTANCE.dpToUnitPx(8), Utils.INSTANCE.dpToUnitPx(8));
        GridLayoutManager gridLayoutManager =
                new GridLayoutManager(this,   2);
        binding.recycler.setLayoutManager(gridLayoutManager);
        binding.recycler.setAdapter(ClassAdapter);
        binding.recycler.addItemDecoration(itemSpace);
//        ClassAdapter.setListener(this);
    }

    @Override
    public void onHomeClick(ThemeData themeData) {
        Intent setIntent = new Intent(this, SetThemesActivity.class);
        setIntent.putExtra(ConstValue.KEYTheme, themeData);
        startActivity(setIntent);
    }


}