package com.example.FinanceTracker.Controllers;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.FinanceTracker.DTOs.Investments.CurrRate;
import com.example.FinanceTracker.DTOs.Investments.Currency;
import com.example.FinanceTracker.DTOs.Investments.ExchangeRate;
import com.example.FinanceTracker.Services.InvestmentService;

@Controller
public class InvestmentsController {

    @Autowired
    private InvestmentService investmentService;

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

        // Create Currency Pills
        List<CurrRate> allRates = investmentService.getCurrRates();
        model.addAttribute("currRates", allRates);

        // Populate Historical Rates Dropdown
        List<Currency> allCurrencies = investmentService.getAllCurrencies();
        model.addAttribute("allCurrencies", allCurrencies);

        // historical rates chart
        List<ExchangeRate> historicalExchangeRates = investmentService.getHistoricalRates("USD", 1).reversed();

        // Get all the rates from the historical Exchange Rates
        ArrayList<BigDecimal> historicalRates = new ArrayList<>();
        ArrayList<LocalDate> historicalRatesLabels = new ArrayList<>();
        for (ExchangeRate exchangeRate : historicalExchangeRates) {
            historicalRates.add(exchangeRate.getRate());
            historicalRatesLabels.add(exchangeRate.getCreatedAt().toLocalDate());
        }

        model.addAttribute("historicalRates", historicalRates);
        model.addAttribute("labels", historicalRatesLabels);

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

        return "investments";
    }

    @GetMapping("/historicalRatesWeekly")
    public String showHistoricalRatesWeek(Model model, 
        @RequestParam("historicalRatesWeekly") boolean historicalRatesWeekly){

        // historical rates chart
        List<ExchangeRate> historicalExchangeRates = investmentService.getHistoricalRates("USD", 3).reversed();

        // Get all the rates from the historical Exchange Rates
        ArrayList<BigDecimal> historicalRates = new ArrayList<>();
        ArrayList<LocalDate> historicalRatesLabels = new ArrayList<>();
        for (ExchangeRate exchangeRate : historicalExchangeRates) {
            historicalRates.add(exchangeRate.getRate());
            historicalRatesLabels.add(exchangeRate.getCreatedAt().toLocalDate());
        }

        model.addAttribute("historicalRates", historicalRates);
        model.addAttribute("labels", historicalRatesLabels);

        return "investments :: HistoricalRatesChart";
    }


    @GetMapping("/changeHistoricalCurr")
    public String changeHistoricalCurr(Model model, 
        @RequestParam("currencyChange") String currencyChange){

        // historical rates chart
        List<ExchangeRate> historicalExchangeRates = investmentService.getHistoricalRates(currencyChange, 3).reversed();

        // Get all the rates from the historical Exchange Rates
        ArrayList<BigDecimal> historicalRates = new ArrayList<>();
        ArrayList<LocalDate> historicalRatesLabels = new ArrayList<>();
        for (ExchangeRate exchangeRate : historicalExchangeRates) {
            historicalRates.add(exchangeRate.getRate());
            historicalRatesLabels.add(exchangeRate.getCreatedAt().toLocalDate());
        }

        model.addAttribute("historicalRates", historicalRates);
        model.addAttribute("labels", historicalRatesLabels);

        return "investments :: HistoricalRatesChart";
    }




}
