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

public class Missions : MonoBehaviour{
    //For Home Missions, Displaying Missions in Home
    public GameObject Home_missionContainer; // Reference to the Character_Selection canvas
    public GameObject Home_missionPrefab; // Mission prefab
    private MissionProgress missionProgress, retrieved_missionProgress;
    private List<MissionInfo> missionInfo, missionsInWeek;
    private const string player_missionData = "Player_MissionData";


    void OnEnable(){
        //For Home Missions
        if (PlayerPrefs.HasKey(player_missionData)){
            missionProgress = LoadPlayer_MissionData();

        }else{ //no rewards data saved, start somewhere, save and load
            missionProgress = new MissionProgress { start_date = (DateTime.Now).ToString("MM/dd/yyyy hh:mm:ss"), current_week = 1, current_month = 1, completed_missions = new List<int> { } };
        }

        missionsInWeek = getWeeklyMissions(missionProgress.current_week);

        string result1 = ListToString(missionsInWeek);
        //Debug.Log("Missions = " + result1);


        if ((Home_missionContainer != null) && (Home_missionPrefab != null)){
            foreach (MissionInfo mission_info in missionsInWeek){
                // Instantiate the mission prefab
                GameObject homeMission_instance = Instantiate(Home_missionPrefab, Home_missionContainer.transform);
                homeMission_instance.name = "homeMission";

                TextMeshProUGUI homeMissionTitle = homeMission_instance.transform.Find("missionDetails").GetComponent<TextMeshProUGUI>();
                homeMissionTitle.text = mission_info.missionTitle;

                TextMeshProUGUI homeMissionXP = homeMission_instance.transform.Find("missionXP").GetComponent<TextMeshProUGUI>();
                homeMissionXP.text = "+ " + (mission_info.missionXP).ToString() + " XP";

            }

            Home_missionPrefab.SetActive (false);
        }
    }


    string ListToString<T>(List<T> list){
        // Use string.Join to concatenate the elements of the list into a string
        return string.Join(", ", list);
    }


	void Awake(){
		Start();
	}


	void Start(){
		// Missions in the game, more can be added as needed later on!
        missionInfo = new List<MissionInfo>
        {
            //A SEASON runs for 3 MONTHS, Each Week has 5 missions total
            //MONTH 1
            #region
                //WEEK 1, 400XP
                new MissionInfo { missionID = 1, missionTitle = "Survive the first 20 seconds of the game without taking a hit", missionDescription = "Survive for 20 seconds", missionXP = 100, missionProgressCount = 3, month = 1, week = 1},
                new MissionInfo { missionID = 2, missionTitle = "Punch opponent 5 times in a row", missionDescription = "5 consecutive punches", missionXP = 50, missionProgressCount = 3, month = 1, week = 1},
                new MissionInfo { missionID = 3, missionTitle = "Kick opponent 3 times in a row", missionDescription = "3 consecutive kicks", missionXP = 50, missionProgressCount = 3, month = 1, week = 1},
                new MissionInfo { missionID = 4, missionTitle = "Perform Kick-Punch-Kick combo", missionDescription = "KPK combo Hit", missionXP = 100, missionProgressCount = 3, month = 1, week = 1},
                new MissionInfo { missionID = 5, missionTitle = "Win 2 rounds in a row", missionDescription = "Win 2 consecutive rounds", missionXP = 100, missionProgressCount = 2, month = 1, week = 1},

                //WEEK 2, 400XP | Total 800XP
                new MissionInfo { missionID = 6, missionTitle = "Complete any 2 missions", missionDescription = "Complete 2 missions in this week", missionXP = 50, missionProgressCount = 2, month = 1, week = 2},
                new MissionInfo { missionID = 7, missionTitle = "Win 2 matches in a row", missionDescription = "Win 2 consecutive matches", missionXP = 100, missionProgressCount = 2, month = 1, week = 2},
                new MissionInfo { missionID = 8, missionTitle = "Win a round with health not less than 50%", missionDescription = "Win with health less than 50%", missionXP = 50, missionProgressCount = 3, month = 1, week = 2},
                new MissionInfo { missionID = 9, missionTitle = "Reach 500 XP", missionDescription = "Reach 200 XP", missionXP = 50, missionProgressCount = 3, month = 1, week = 2},
                new MissionInfo { missionID = 10, missionTitle = "Rank up to Level 2 Bronze", missionDescription = "Reach Level 2 Bronze", missionXP = 150, missionProgressCount = 3, month = 1, week = 2},

                //WEEK 3, 500XP | Total 1300XP
                new MissionInfo { missionID = 11, missionTitle = "Land a 5-hit punch combo", missionDescription = "Land 5 consecutive Punch hits", missionXP = 100, missionProgressCount = 3, month = 1, week = 3},
                new MissionInfo { missionID = 12, missionTitle = "Win a Perfect round", missionDescription = "Win a round without taking damage (Perfect round)", missionXP = 100, missionProgressCount = 3, month = 1, week = 3},
                new MissionInfo { missionID = 13, missionTitle = "Complete a match in under 30 seconds", missionDescription = "Complete a match under 30 seconds", missionXP = 200, missionProgressCount = 3, month = 1, week = 3},
                new MissionInfo { missionID = 14, missionTitle = "Complete 3 missions in a single match", missionDescription = "Complete 3 missions in a single match", missionXP = 50, missionProgressCount = 3, month = 1, week = 3},
                new MissionInfo { missionID = 15, missionTitle = "Win a round using only punches", missionDescription = "Win a round with punches", missionXP = 50, missionProgressCount = 3, month = 1, week = 3},
                
                //WEEK 4, 500XP | Total 1800XP
                new MissionInfo { missionID = 16, missionTitle = "Win with 10% health remaining", missionDescription = "Win a round with less than 10% health remaining", missionXP = 50, missionProgressCount = 3, month = 1, week = 4},
                new MissionInfo { missionID = 17, missionTitle = "Achieve a win streak of 5 matches in a row", missionDescription = "Win 2 consecutive versus battles", missionXP = 200, missionProgressCount = 3, month = 1, week = 4},
                new MissionInfo { missionID = 18, missionTitle = "Watch 2 Ads", missionDescription = "Watch 2 Ads", missionXP = 50, missionProgressCount = 2, month = 1, week = 4},
                new MissionInfo { missionID = 19, missionTitle = "Reach 1600 XP", missionDescription = "Reach 1500 XP", missionXP = 50, missionProgressCount = 3, month = 1, week = 4},
                new MissionInfo { missionID = 20, missionTitle = "Rank up to Level 2 Silver", missionDescription = "Rank up to Level 2 Silver", missionXP = 150, missionProgressCount = 3, month = 1, week = 4},
            #endregion


            //MONTH 2
            #region
                //WEEEK 1, 500XP | Total 2300XP
                new MissionInfo { missionID = 21, missionTitle = "Win a match without walking around", missionDescription = "Win a match without walking around", missionXP = 100, missionProgressCount = 3, month = 2, week = 1},
                new MissionInfo { missionID = 22, missionTitle = "Watch 10 Ads", missionDescription = "Watch 10 Ads", missionXP = 50, missionProgressCount = 10, month = 2, week = 1},
                new MissionInfo { missionID = 23, missionTitle = "Achieve a perfect round using only kicks", missionDescription = "Achieve a perfect round using only kicks", missionXP = 100, missionProgressCount = 3, month = 2, week = 1},
                new MissionInfo { missionID = 24, missionTitle = "Win a round sustaining less than 5 hits from opponent", missionDescription = "Win a round sustaining less than 5 hits from opponent", missionXP = 150, missionProgressCount = 3, month = 2, week = 1},
                new MissionInfo { missionID = 25, missionTitle = "Win a match using only punches", missionDescription = "Win a match using only punches", missionXP = 100, missionProgressCount = 3, month = 2, week = 1},

                //WEEK 2, 400XP | Total 2700XP
                new MissionInfo { missionID = 26, missionTitle = "Win a match with not less than 60% health in the final round", missionDescription = "Win a match with not less than 60% health in the final round", missionXP = 50, missionProgressCount = 3, month = 2, week = 2},
                new MissionInfo { missionID = 27, missionTitle = "Perform Punch-Kick-Kick combo", missionDescription = "Perform Punch-Kick-Kick combo", missionXP = 100, missionProgressCount = 3, month = 2, week = 2},
                new MissionInfo { missionID = 28, missionTitle = "Reach 2400 XP", missionDescription = "Reach 2400 XP", missionXP = 50, missionProgressCount = 3, month = 2, week = 2},
                new MissionInfo { missionID = 29, missionTitle = "Rank up to Level 2 Gold", missionDescription = "Rank up to Level 2 Gold", missionXP = 150, missionProgressCount = 3, month = 2, week = 2},
                new MissionInfo { missionID = 30, missionTitle = "Complete this week’s missions", missionDescription = "Complete this week’s missions", missionXP = 50, missionProgressCount = 3, month = 2, week = 2},

                //WEEK 3, 300XP | Total 3000XP
                new MissionInfo { missionID = 31, missionTitle = "Complete a round in under 30 seconds", missionDescription = "Complete a round in under 30 seconds", missionXP = 50, missionProgressCount = 3, month = 2, week = 3},
                new MissionInfo { missionID = 32, missionTitle = "Watch 5 Ads", missionDescription = "Win a match using a character you've never played before", missionXP = 50, missionProgressCount = 5, month = 2, week = 3},
                new MissionInfo { missionID = 33, missionTitle = "Achieve a win with less than 5% health remaining in each round", missionDescription = "Achieve a win with less than 5% health remaining in each round", missionXP = 100, missionProgressCount = 3, month = 2, week = 3},
                new MissionInfo { missionID = 34, missionTitle = "Win a round with more 75% health remaining", missionDescription = "Win a round with more 75% health remaining", missionXP = 50, missionProgressCount = 3, month = 2, week = 3},
                new MissionInfo { missionID = 35, missionTitle = "Complete this week’s missions", missionDescription = "Complete this week’s missions", missionXP = 50, missionProgressCount = 3, month = 2, week = 3},
                
                //WEEK 4, 900XP | Total 3900XP
                new MissionInfo { missionID = 36, missionTitle = "Play different matches for a total of 30 minutes", missionDescription = "Play different matches for a total of 30 minutes", missionXP = 500, missionProgressCount = 3, month = 2, week = 4},
                new MissionInfo { missionID = 37, missionTitle = "Watch 15 Ads", missionDescription = "Watch 15 Ads", missionXP = 50, missionProgressCount = 15, month = 2, week = 4},
                new MissionInfo { missionID = 38, missionTitle = "Perform Punch-Punch-Kick attack", missionDescription = "Perform Punch-Punch-Kick attack", missionXP = 100, missionProgressCount = 3, month = 2, week = 4},
                new MissionInfo { missionID = 39, missionTitle = "Reach 3600 XP", missionDescription = "Reach 3600 XP", missionXP = 50, missionProgressCount = 3, month = 2, week = 4},
                new MissionInfo { missionID = 40, missionTitle = "Rank up to Level 1 Platinum", missionDescription = "Rank up to Level 1 Platinum", missionXP = 200, missionProgressCount = 3, month = 2, week = 4},
            #endregion


            //MONTH 3
            #region
                //WEEK 1, 500XP | Total 4400XP
                new MissionInfo { missionID = 41, missionTitle = "Perform a combo that does at least 30% damage", missionDescription = "Perform a combo that does at least 30% damage", missionXP = 100, missionProgressCount = 3, month = 3, week = 1},
                new MissionInfo { missionID = 42, missionTitle = "Win a round with a health difference of less than 10% between you and your opponent", missionDescription = "Win a round with a health difference of less than 10% between you and your opponent", missionXP = 150, missionProgressCount = 3, month = 3, week = 1},
                new MissionInfo { missionID = 43, missionTitle = "Reach 4200 XP", missionDescription = "Reach 4200 XP", missionXP = 50, missionProgressCount = 3, month = 3, week = 1},
                new MissionInfo { missionID = 44, missionTitle = "Rank up to Level 3 Platinum", missionDescription = "Rank up to Level 3 Platinum", missionXP = 150, missionProgressCount = 3, month = 3, week = 1},
                new MissionInfo { missionID = 45, missionTitle = "Complete this week's missions", missionDescription = "Complete this week's missions", missionXP = 50, missionProgressCount = 3, month = 3, week = 1},

                //WEEK 2, 600XP | Total 5000XP
                new MissionInfo { missionID = 46, missionTitle = "Achieve a win streak of 7 matches in a row", missionDescription = "Achieve a win streak of 7 matches in a row", missionXP = 200, missionProgressCount = 3, month = 3, week = 2},
                new MissionInfo { missionID = 47, missionTitle = "Win each round your character's health below 25%", missionDescription = "Win each round your character's health below 25%", missionXP = 150, missionProgressCount = 3, month = 3, week = 2},
                new MissionInfo { missionID = 48, missionTitle = "Reach 4800 XP", missionDescription = "Reach 4800 XP", missionXP = 50, missionProgressCount = 3, month = 3, week = 2},
                new MissionInfo { missionID = 49, missionTitle = "Rank up to Level 2 Diamond", missionDescription = "Rank up to Level 2 Diamond", missionXP = 250, missionProgressCount = 3, month = 3, week = 2},
                new MissionInfo { missionID = 50, missionTitle = "Complete this week's missions", missionDescription = "Complete this week's missions", missionXP = 50, missionProgressCount = 3, month = 3, week = 2},

                //WEEK 3, 500XP | Total 5500XP
                new MissionInfo { missionID = 51, missionTitle = "Complete all 3 rounds in under 3 minutes", missionDescription = "Complete all 3 rounds in under 3 minutes", missionXP = 150, missionProgressCount = 3, month = 3, week = 3},
                new MissionInfo { missionID = 52, missionTitle = "Watch 20 Ads", missionDescription = "Watch 20 Ads", missionXP = 100, missionProgressCount = 20, month = 3, week = 3},
                new MissionInfo { missionID = 53, missionTitle = "Reach 1000 XP", missionDescription = "Reach 1000 XP", missionXP = 50, missionProgressCount = 3, month = 3, week = 3},
                new MissionInfo { missionID = 54, missionTitle = "Rank up to Master Level", missionDescription = "Rank up to Master Level", missionXP = 150, missionProgressCount = 3, month = 3, week = 3},
                new MissionInfo { missionID = 55, missionTitle = "Complete this week's missions", missionDescription = "Complete this week's missions", missionXP = 50, missionProgressCount = 3, month = 3, week = 3},
                
                //WEEK 4, 500XP | Total 6000XP
                new MissionInfo { missionID = 56, missionTitle = "Complete 2 rounds in under 1 minute", missionDescription = "Complete 2 rounds in under 1 minute", missionXP = 100, missionProgressCount = 3, month = 3, week = 4},
                new MissionInfo { missionID = 57, missionTitle = "Defeat opponent using only Kick-Punch move", missionDescription = "Defeat opponent using only Kick-Punch move", missionXP = 100, missionProgressCount = 3, month = 3, week = 4},
                new MissionInfo { missionID = 58, missionTitle = "Reach 1200 XP", missionDescription = "Reach 1200 XP", missionXP = 50, missionProgressCount = 3, month = 3, week = 4},
                new MissionInfo { missionID = 59, missionTitle = "Rank up to Apex Predator Level", missionDescription = "Rank up to Apex Predator Level", missionXP = 200, missionProgressCount = 3, month = 3, week = 4},
                new MissionInfo { missionID = 60, missionTitle = "Complete this week's missions", missionDescription = "Complete this week's missions", missionXP = 50, missionProgressCount = 3, month = 3, week = 4},
            #endregion

        };
	}


    // Function that return XP of a mission
	public int getmission_XP(int missionID_updateCompletion, int type_of_mission){
        Start();

		int xp = 0;
		if (type_of_mission == 0){
			MissionInfo mission = missionInfo.Find(m => m.missionID == missionID_updateCompletion);
			xp = mission.missionXP;
			
		}
		return xp;
	}


    // Function to get Weekly Missions based on the current week
    public List<MissionInfo> getWeeklyMissions(int week_num){
        Start();

        retrieved_missionProgress = LoadPlayer_MissionData();
        int retrieved_CurrentWeek = retrieved_missionProgress.current_week;
        int retrieved_CurrentMonth = retrieved_missionProgress.current_month;

        // missionsInWeek = missionInfo.Where(mission => mission.week == 1).ToList();

        //Get missions for month 1 and week 1
        //Alter this so as to recall from PlayerPrefs saved data
        missionsInWeek = missionInfo.Where(mission => ((mission.month == retrieved_CurrentMonth) && (mission.week == retrieved_CurrentWeek))).ToList();

        return missionsInWeek;
    }

    public List<MissionInfo> getAllWeeklyMissions(){
        Start();
        missionsInWeek = missionInfo.ToList();
        // string result1 = ListToString(missionsInWeek);

        return missionsInWeek;
    }


    // Function to get Mission Title
    public string getMissionTitle(int missionID){
        Start();
        MissionInfo mission = missionInfo.Find(m => m.missionID == missionID);

        return mission.missionTitle;
    }

    // Function to get Progress Count of missions
    public int getMissionProgressCount(int missionID){
        Start();
        MissionInfo mission = missionInfo.Find(m => m.missionID == missionID);

        return mission.missionProgressCount;
    }


    // ***** PLAYERPREFS used in this script
    //Loads mission progress from PlayerPrefs.
    MissionProgress LoadPlayer_MissionData(){
        string jsonData = PlayerPrefs.GetString(player_missionData, "{}"); // Details were stored as "PlayerData"
        return JsonUtility.FromJson<MissionProgress>(jsonData);
    }

}