package com.makeeasy.dpandstatus;

import static android.widget.Toast.LENGTH_SHORT;
import static android.widget.Toast.makeText;
import static com.makeeasy.dpandstatus.Constant.getAdSize;
import static java.util.Locale.getDefault;

import android.os.Bundle;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.AdapterStatus;
import com.makeeasy.dpandstatus.adapter.PhotoAdapter;
import com.makeeasy.dpandstatus.model.Photo;
import com.makeeasy.dpandstatus.service.FavouriteList;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

public class FavoriteActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private SwipeRefreshLayout refreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorite);
        MobileAds.initialize(this, initializationStatus -> {
            Map<String, AdapterStatus> statusMap = initializationStatus.getAdapterStatusMap();
            for (String adapterClass : statusMap.keySet()) {
                AdapterStatus status = statusMap.get(adapterClass);
                assert status != null;
            }
            AdView adView = new AdView(this);
            adView.setAdUnitId(getString(R.string.banner_id));
            ((FrameLayout)findViewById(R.id.adView)).addView(adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            adView.setAdSize(getAdSize(this));
            adView.loadAd(adRequest);
        });
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if(getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        toolbar.setNavigationOnClickListener(view -> finish());
        recyclerView = findViewById(R.id.list);
        refreshLayout = findViewById(R.id.swipeContainer);
        refreshLayout.setOnRefreshListener(this::getPhotosData);
        refreshLayout.setRefreshing(true);
        getPhotosData();
    }

    private void getPhotosData() {
        Map<String, ?> allEntries = FavouriteList.getAll(this);
        if(allEntries.isEmpty()) {
            refreshLayout.setRefreshing(false);
            makeText(this,"Favourite list is empty",LENGTH_SHORT).show();
        } else {
            List<Object> photoList = new ArrayList<>();
            String date = new SimpleDateFormat("dd/MM/yyyy",getDefault()).format(Calendar.getInstance().getTime());
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                photoList.add(new Photo(entry.getKey(),"image",entry.getValue().toString(),date, "Favourite"));
            }
            recyclerView.setAdapter(new PhotoAdapter(this,photoList));
            refreshLayout.setRefreshing(false);
        }
    }
}