package com.photo.wallpaper.category.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import com.photo.wallpaper.category.ad.AdManager;
import com.photo.wallpaper.category.ad.onActionListener;
import com.photo.wallpaper.category.api.ApiItemClick;
import com.photo.wallpaper.category.bean.Data;
import com.photo.wallpaper.category.bean.DataBaseHelper;
import com.photo.wallpaper.category.databinding.ActivityLayoutClassBinding;
import com.photo.wallpaper.category.itemAdapter.AdThumb;
import com.photo.wallpaper.category.utils.CommonHelper;
import com.photo.wallpaper.category.utils.ItemSpace;

import java.util.List;

public class ActivityCategory extends AppCompatActivity implements ApiItemClick {
    private ActivityLayoutClassBinding vb;

    private String categoryName;
    public static final String key_index = "index";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityLayoutClassBinding.inflate(getLayoutInflater());
        setContentView(vb.getRoot());
        CommonHelper.initFull(this, false);
        AdManager.loadAllAd();
        categoryName = getIntent().getStringExtra(key_index);
        vb.name.setText(categoryName);
        initList();
        vb.imageviewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AdManager.showTopOn(ActivityCategory.this, new onActionListener() {
            @Override
            public void onAction() {

            }
        });
    }

    private void initList() {
        List<Data> data = DataBaseHelper.queryCategoryAll(categoryName);
        ItemSpace itemSpace = new ItemSpace(10, 10, 0);
//        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        vb.list.addItemDecoration(itemSpace);
        vb.list.setLayoutManager(gridLayoutManager);
        int height = CommonHelper.dp2Px(180);
        AdThumb adThumb = new AdThumb(this, height);
        adThumb.setDataInfo(data);
        adThumb.setInfoWallpaperListener(this);
        vb.list.setAdapter(adThumb);

    }

    @Override
    public void onItemClickAction(Data info) {
        Intent intent = new Intent(this, ActivityApply.class);
        intent.putExtra(CommonHelper.key_info, info);
        startActivity(intent);
    }
}