package com.wall.wallpaper.hd.ui_view;

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.wallpaper.hd.ad.AdManager;
import com.wall.wallpaper.hd.ad.onActionListener;
import com.wall.wallpaper.hd.adapter.AdapterList;
import com.wall.wallpaper.hd.databinding.ActivityWallListBinding;
import com.wall.wallpaper.hd.javabean.Data;
import com.wall.wallpaper.hd.listener.ItemClickListener;
import com.wall.wallpaper.hd.tools.CommonUtils;
import com.wall.wallpaper.hd.tools.DbManager;
import com.wall.wallpaper.hd.tools.ItemSpace;

import java.util.List;

public class ActivityWallList extends AppCompatActivity implements ItemClickListener {
    private ActivityWallListBinding vb;

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

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

                AdManager.showTopOn(ActivityWallList.this, new onActionListener() {
                    @Override
                    public void onAction() {
                        finish();
                    }
                });

            }
        });

        AdManager.showTopOn(ActivityWallList.this, new onActionListener() {
            @Override
            public void onAction() {

            }
        });
    }

    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, 2);
        vb.list.addItemDecoration(itemSpace);
        vb.list.setLayoutManager(gridLayoutManager);
        int height = CommonUtils.dp2Px(180);
        AdapterList adapterList = new AdapterList(this, height);
        adapterList.setDataInfo(data);
        adapterList.setInfoWallpaperListener(this);
        vb.list.setAdapter(adapterList);

    }

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