package com.example.FinanceTracker.Controllers;

import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.example.FinanceTracker.Models.Transaction;
import com.example.FinanceTracker.Services.TransactionService;

@Controller
public class TransactionsController {
        private int pageNum = 0;
        private int pageSize = 10;

        @Autowired
        private TransactionService transactionService;

        @GetMapping("/transactions")
        public String transactions(Model model) {

                Page<Transaction> pageResults = transactionService.findAllPaged(pageNum, pageSize);

                model.addAttribute("pageNum", pageNum);
                model.addAttribute("pageSize", pageResults.getTotalPages());
                model.addAttribute("transactionCount", pageResults.getNumberOfElements());
                model.addAttribute("transactions", pageResults);

                // footer
                model.addAttribute("footerDetails", "Copyright © FinanceTracker " + LocalDate.now().getYear());

                return "transactions";
        }

        @GetMapping("/changePageSize")
        public Collection<ModelAndView> changePageSize(
                        @RequestParam("pageSizeCB") int pageSizeCB) {
                this.pageSize = pageSizeCB;
                this.pageNum = 0;

                Page<Transaction> results = transactionService.findAllPaged(pageNum, pageSize);
                return List.of(
                                new ModelAndView("transactions :: transactionTable",
                                                Map.of("transactions", results)),
                                setPageDetails(results));
        }

        @GetMapping("/searchTransactions")
        public Collection<ModelAndView> SearchTable(@RequestParam("searchValue") String searchValue) {
                this.pageNum = 0;
                Page<Transaction> searchResults = transactionService.searchTransactions(searchValue, pageNum, pageSize);

                return List.of(
                                new ModelAndView("transactions :: transactionTable",
                                                Map.of("transactions", searchResults)),
                                setPageDetails(searchResults));
        }

        @GetMapping("/previousPage")
        public Collection<ModelAndView> previousPage() {

                if (pageNum > 0) {
                        pageNum--;
                }
                Page<Transaction> results = transactionService.findAllPaged(pageNum, pageSize);
                return List.of(
                                new ModelAndView("transactions :: transactionTable",
                                                Map.of("transactions", results)),
                                setPageDetails(results));
        }

        @GetMapping("/nextPage")
        public Collection<ModelAndView> nextPage() {

                Page<Transaction> current = transactionService.findAllPaged(pageNum, pageSize);

                if (pageNum < current.getTotalPages() - 1) {
                        pageNum++;
                }
                Page<Transaction> results = transactionService.findAllPaged(pageNum, pageSize);
                return List.of(
                                new ModelAndView("transactions :: transactionTable",
                                                Map.of("transactions", results)),
                                setPageDetails(results));
        }

        private ModelAndView setPageDetails(Page<?> page) {
                return new ModelAndView("transactions :: tableInfo",
                                Map.of("pageNum", pageNum, "pageSize", page.getTotalPages(), "transactionCount",
                                                page.getNumberOfElements()));

        }

}
