package com.newapp.paperwall.uiview;

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

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


import com.newapp.paperwall.adapter.ThreeLinesAdapter;
import com.newapp.paperwall.databinding.ActivityCategoryListBinding;
import com.newapp.paperwall.javabean.Data;
import com.newapp.paperwall.listener.OnItemClickListener;
import com.newapp.paperwall.tools.UiUtils;
import com.newapp.paperwall.tools.DbManager;
import com.newapp.paperwall.tools.ItemSpace;

import java.util.List;

public class CategoryListActivity extends AppCompatActivity implements OnItemClickListener {
    private ActivityCategoryListBinding vb;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = ActivityCategoryListBinding.inflate(getLayoutInflater());
        setContentView(vb.getRoot());
        UiUtils.initFull(this, true);
        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() {
        List<Data> data = DbManager.queryCategoryAll(categoryName);
        ItemSpace itemSpace = new ItemSpace(10, 10, 0);
//        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        vb.list.addItemDecoration(itemSpace);
        vb.list.setLayoutManager(gridLayoutManager);
        int height = UiUtils.dp2Px(240);
        ThreeLinesAdapter threeLinesAdapter = new ThreeLinesAdapter(this, height);
        threeLinesAdapter.setDataInfo(data);
        threeLinesAdapter.setInfoWallpaperListener(this);
        vb.list.setAdapter(threeLinesAdapter);

    }

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