package com.wall.photography.picture.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.wall.photography.picture.databinding.ActivityListBinding;
import com.wall.photography.picture.tool.CommonUtils;
import com.wall.photography.picture.tool.ObjectBoxManager;
import com.wall.photography.picture.tool.GridItemDecoration;
import com.wall.photography.picture.Connect.ItemClick;
import com.wall.photography.picture.Data.Data;

public class List extends AppCompatActivity implements ItemClick {
    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());
        CommonUtils.initFull(this, false);
        categoryName = getIntent().getStringExtra(key_index);
        vb.name.setText(categoryName);
        initList();
        vb.imageviewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initList() {
        java.util.List<Data> data;
        // 如果是标签（Feature、Animal、Film），显示所有壁纸
        if (categoryName != null && (categoryName.equals("Feature") || categoryName.equals("Animal") || categoryName.equals("Film"))) {
            data = ObjectBoxManager.queryWallpapersByTag(categoryName, 500);
        } else {
            // 否则按分类查询
            data = ObjectBoxManager.queryCategoryAll(categoryName);
        }
        GridItemDecoration itemHelper = new GridItemDecoration(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 = CommonUtils.dp2Px(180);
        com.wall.photography.picture.Project.List itemList = new com.wall.photography.picture.Project.List(this, height,false);
        itemList.setDataInfo(data);
        itemList.setInfoWallpaperListener(this);
        vb.list.setAdapter(itemList);

    }

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