using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

/*
    DESCRIPTION:
    1. announcement_progression has buttons which are used to navigate throught the content in announcement_content
    2. The logic of this script is as follows:
            - announcement_progression[1] only is visible when showing content in announcement_content[0]
            - announcement_progression[0] and announcement_progression[1] should be visible when showing content in announcement_content[1]
            - announcement_progression[0] and announcement_progression[2] should be visible when showing content in announcement_content[3]

*/


public class Announcement : MonoBehaviour
{
    public Button[] announcement_progression; //Buttons for navigating throught the Announcement Content | 0. Left, 1. Right
    public GameObject[] announcement_content; //Gameobjects to be shown for the Announcement Content | 0, 1, 2
    private int currentIndex = 0; //Which Announcement content is currently being shown
    [SerializeField]
    GameObject homeScreen;

    void OnEnable(){
        // Set all content to false/not visible
        foreach (GameObject obj in announcement_content){
            obj.SetActive(false);
        }

        currentIndex = 0; //Start with the first GameObject/Announcement Content 
        
        // Initialize the content visibility
        UpdateContentVisibility();
    }


    // Start is called before the first frame update
    void Start()
    {

        // Initialize button click listeners, for the Next and Previous buttons
        announcement_progression[0].onClick.AddListener(ShowPrevious);
        announcement_progression[1].onClick.AddListener(ShowNext);


        // Change UI after Announcement, "Next" Button, in the last page of the Announcements
        announcement_progression[2].onClick.AddListener(AfterAnnouncement);
        //select the right arrow key
        announcement_progression[1].Select();
        // PlayerPrefs code, Check the status of the PLAYERPREFS related to Announcement, referenced as "announcementStatus"
        // Check if the "announcement" key exists in PlayerPrefs
        if (PlayerPrefs.HasKey("announcementStatus")){

            // Get the value of the "announcement" key
            string announcementValue = PlayerPrefs.GetString("announcementStatus");

            // Check if the value is "0", if it is 0 means the Announcements have not yet been shown in the game
            /*if (announcementValue == "0"){
                Debug.Log("The 'announcement' key is assigned and its value is '0'.");
            }
            else{
                Debug.Log("The 'announcement' key is assigned but its value is not '0'.");
            }*/
        }

        else{
            PlayerPrefs.SetString("announcementStatus", "0");
            //Debug.Log("The 'announcement' key is not assigned.");
           
        }        
    }


    //Function for the button to go to the Previous Announcement Content
    void ShowPrevious()
    {
        if (currentIndex <= 0)
        {
            // currentIndex = announcement_content.Length - 1; // Wrap around to the last element
            currentIndex = currentIndex; // Wrap around to the last element

        }else{
            // Decrease the index
            currentIndex--;
        }
        announcement_progression[0].Select();
        if(currentIndex == 0)//if on the first page
            announcement_progression[1].Select();
        // Update the content visibility
        UpdateContentVisibility();
        print("pressed");
    }


    //Function for the button to go to the Next Announcement Content
    void ShowNext()
    {
        if (currentIndex == (announcement_content.Length - 1))
        {
            // currentIndex = 0; // Wrap around to the first element
            currentIndex = currentIndex; // Wrap around to the first element

        }else{
            // Increase the index
            currentIndex++;
        }
        announcement_progression[1].Select();
        if(currentIndex == 2)//if on the last page
            announcement_progression[0].Select();
        // Update the content visibility
        UpdateContentVisibility();
        print("pressed");
    }


    //After the last page of the Announcements, set the status to 1
    void AfterAnnouncement(){
        PlayerPrefs.SetString("announcementStatus", "1"); //Announcements Done
        gameObject.SetActive(false); //Set Announcements UI to false
        homeScreen.GetComponent<HomeSrceen>().primaryButton.Select();
        print("pressed");
    }


    //Function to Show the Announcement content based on the current index
    void UpdateContentVisibility()
    {
        // Hide all content elements
        for (int i = 0; i < announcement_content.Length; i++){
            announcement_content[i].SetActive(false);
        }

        // Show the current content element
        if (announcement_content.Length > 0){
            announcement_content[currentIndex].SetActive(true);
        }

        // Shows the first Announcement content
        if (currentIndex <= 0){
            announcement_content[0].SetActive(true); //Sets the first element to be visible

            ShowOrHideButton(announcement_progression[0], false);
            ShowOrHideButton(announcement_progression[2], false);
        }

        // Shows the second Announcement content
        if ((currentIndex > 0) && (currentIndex < (announcement_content.Length - 1))){
            ShowOrHideButton(announcement_progression[0], true);
            ShowOrHideButton(announcement_progression[1], true);
            ShowOrHideButton(announcement_progression[2], false);
        }

        // Shows the third Announcement content
        if (currentIndex >= (announcement_content.Length - 1)){
            announcement_content[2].SetActive(true); //Sets the third element to be visible

            //Takes you to next UI here
            ShowOrHideButton(announcement_progression[1], false);
            ShowOrHideButton(announcement_progression[2], true);
        }
    }


    //Function to dynamically set the visibility of gameobject which takes a button as an argument
    void ShowOrHideButton(Button btn, bool buttonStatus){
        ((btn).gameObject).SetActive(buttonStatus);
    }
}
