package com.lh.wallpaper2.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.lh.wallpaper2.DetailsActivity;
import com.lh.wallpaper2.R;
import com.lh.wallpaper2.SettingActivity;
import com.lh.wallpaper2.StaticValue;
import com.lh.wallpaper2.ViewActivity;
import com.lh.wallpaper2.Wallpaper;
import com.lh.wallpaper2.action.InfoWallpaperListener;
import com.lh.wallpaper2.adapter.InfoWallpaperAdapter;
import com.lh.wallpaper2.json.Info;
import com.lh.wallpaper2.json.Url;

public class HomeFragment extends Fragment implements InfoWallpaperListener {
    private RecyclerView recyclerView;
    private Info info;
    private RelativeLayout relativeLayout;
    private ImageView imageView;
    private ImageView setting;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        recyclerView = view.findViewById(R.id.home_recyclerView);
        relativeLayout = view.findViewById(R.id.top_info);
        setting = view.findViewById(R.id.setting);
        info = Wallpaper.getUserList().get(14);
        String topInfo = info.getList().get(0).getSourceUrl();
        setRecyclerView();
        imageView = view.findViewById(R.id.top_image);
        Glide.with(requireContext()).load(topInfo).into(imageView);
        relativeLayout.setOnClickListener(v -> {
            Intent intent = new Intent(requireContext(), ViewActivity.class);
            intent.putExtra("top_info", topInfo);
            startActivity(intent);
        });
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(requireContext(), SettingActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }

    private void setRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(), 1);
        InfoWallpaperAdapter infoWallpaperAdapter = new InfoWallpaperAdapter(Wallpaper.getUserList(), requireContext());
        infoWallpaperAdapter.setInfoWallpaperListener(this);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(infoWallpaperAdapter);
    }

    @Override
    public void onItemClickAction(Info info) {
        Intent intent = new Intent(requireContext(), DetailsActivity.class);
        intent.putExtra(StaticValue.key_info, info);
        startActivity(intent);
    }

    @Override
    public void onHomeInfoClickAction(Url info) {
        Intent intent = new Intent(requireContext(), ViewActivity.class);
        intent.putExtra("home_info", info);
        startActivity(intent);
    }
}