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

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

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

import com.keyboard.theme.app.keyboard.MyAppllication;
import com.keyboard.theme.app.keyboard.adapter.SingeClassAdapter;
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.ActivityClassBinding;
import com.keyboard.theme.app.keyboard.utils.Common;
import com.keyboard.theme.app.keyboard.utils.ItemSpace;
import com.keyboard.theme.app.keyboard.utils.StaticValue;

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

public class ClassActivity extends BaseActivity<ActivityClassBinding> implements OnHomeApi {

    public static final String KEY = "KEY_class_name";
    private ActivityClassBinding binding;
    private Info classInfo;


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

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

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

        for (Info datum : MyAppllication.Companion.getData()) {
            if(Objects.equals(datum.getClassName(), className)){
                classInfo  = datum;
                Log.d("-----","---datum.getClassName()="+datum.getClassName()+"----className="+className);
                break;
            }
        }

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

    }

    private void initList(){
        List<InfoChild> list = classInfo.getList();
        if(list == null){
            return;
        }
        SingeClassAdapter SingeClassAdapter = new SingeClassAdapter(this, list);
        SingeClassAdapter.setListener(this);
        ItemSpace itemSpace = new ItemSpace(Common.INSTANCE.dpToUnitPx(8), Common.INSTANCE.dpToUnitPx(8), Common.INSTANCE.dpToUnitPx(8));
        GridLayoutManager gridLayoutManager =
                new GridLayoutManager(this,   2);
        binding.recycler.setLayoutManager(gridLayoutManager);
        binding.recycler.setAdapter(SingeClassAdapter);
        binding.recycler.addItemDecoration(itemSpace);
//        SingeClassAdapter.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) {

    }
}