package com.wall.photography.wallpapers.acctivity;

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

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

import com.wall.photography.wallpapers.databinding.ActivityListBinding;
import com.wall.photography.wallpapers.tool.ComUtils;
import com.wall.photography.wallpapers.tool.ObectBoxManager;
import com.wall.photography.wallpapers.tool.ItemHelper;
import com.wall.photography.wallpapers.connector.OnItemClick;
import com.wall.photography.wallpapers.mybean.MyData;
import com.wall.photography.wallpapers.item.ItemList;
import com.wall.photography.wallpapers.topon.AdManager;
import com.wall.photography.wallpapers.topon.onActionListener;

import java.util.List;

public class ListActivity extends AppCompatActivity implements OnItemClick {
    private ActivityListBinding vb;

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

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

                AdManager.showTopOn(ListActivity.this, () -> {
                    finish();
                });

            }
        });
        AdManager.showTopOn(ListActivity.this, () -> {

        });
    }

    private void initList() {
        List<MyData> data = ObectBoxManager.queryCategoryAll(categoryName);
        ItemHelper itemHelper = new ItemHelper(10, 10, 0);
//        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        vb.list.addItemDecoration(itemHelper);
        vb.list.setLayoutManager(gridLayoutManager);
        int height = ComUtils.dp2Px(180);
        ItemList itemList = new ItemList(this, height,false);
        itemList.setDataInfo(data);
        itemList.setInfoWallpaperListener(this);
        vb.list.setAdapter(itemList);

    }

    @Override
    public void onItemClickAction(MyData info) {
        Intent intent = new Intent(this, WallpaperActivity.class);
        intent.putExtra(ComUtils.key_info, info);
        startActivity(intent);
    }
}