using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class BattlePassManager : MonoBehaviour
{

    [Header("Tier Data")]
    public bool isTier1Double;

    public BattlePassTierStruct[] RewardTiers;
    public float numTiers;
    public float numPages;
    public float numPagesFloor;
    public float remTiers;


    public int currentPage;
    public int nextPageNum;
    public int prevPageNum;

    public int startRange;
    public int endRange;

    public Canvas BPCanvas;
    public GameObject PanelA;
    public GameObject PanelAContent;
    public GameObject PanelB;
    public GameObject PanelBContent;

    public GameObject RewardPrefab;

    public Vector3 RewardPosA;
    public string slideDirection;

    public GameObject LivePanel;
    public GameObject ScrapPanel;


    // Use this for initialization
    void Start ()
    {
        //Set Reward Postions
        //Position A - Standard Reward
        //RewardPosA = new Vector2(140,-163);
        //Get the first tier info; is it normal or a double?


        //Get the number of tiers and calculate the number of pages needed.
        numTiers = RewardTiers.Length;

        if (isTier1Double)
        {
            //Do this

        }
        else
        {
            numPages = (Mathf.Floor(numTiers / 8.0f));
            remTiers = (numTiers - (numPages * 8));
        }
        currentPage = 1;
        prevPageNum = 1;

        LivePanel = PanelA;
        ScrapPanel = PanelB;

        FillPages();
	}
	
	// Update is called once per frame
	void Update ()
    {

    }

    public void FillPages()
    {
        //ClearPages();

        RectTransform rt;
        rt = BPCanvas.GetComponent<RectTransform>();

        if (prevPageNum < 1)
        {
            
        }
        else
        {
            endRange = currentPage * 8;
            startRange = endRange - 7;

            for (int i = startRange; i < (endRange) + 1; i++)
            {
                //Get the tier information for each
                if(RewardTiers[i].Tier.standardReward != null)
                {
                    var newRewardPrefab = Instantiate(RewardPrefab, PanelAContent.transform);
                    newRewardPrefab.transform.position = new Vector3((123 * i) + 17, ((rt.rect.height) - 163), 0);
                    var RTM = newRewardPrefab.GetComponent<RewardTemplateManager>();
                    RTM.FillTemplate(RewardTiers[i].Tier.standardReward);
                }

                if (RewardTiers[i].Tier.upperBattleReward != null)
                {
                    var newRewardPrefab = Instantiate(RewardPrefab, PanelAContent.transform);
                    newRewardPrefab.transform.position = new Vector3((123 * i) + 17, ((rt.rect.height) - 337), 0);
                    var RTM = newRewardPrefab.GetComponent<RewardTemplateManager>();
                    RTM.FillTemplate(RewardTiers[i].Tier.upperBattleReward);
                }

                if (RewardTiers[i].Tier.lowerBattleReward != null)
                {
                    var newRewardPrefab = Instantiate(RewardPrefab, PanelAContent.transform);
                    switch (RewardTiers[i].Tier.upperBattleReward.rewardType)
                    {
                        case RewardType.MinorReward:
                            newRewardPrefab.transform.position = new Vector3((123 * i) + 17, ((rt.rect.height) - 441), 0);
                            break;
                        case RewardType.MajorReward:
                            newRewardPrefab.transform.position = new Vector3((123 * i) + 17, ((rt.rect.height) - 495), 0);
                            break;

                        default:
                            break;
                    }
                    var RTM = newRewardPrefab.GetComponent<RewardTemplateManager>();
                    RTM.FillTemplate(RewardTiers[i].Tier.lowerBattleReward);
                }
            }
        }
    }

    public void ClearPages()
    {

    }

    public void SlidePages(string slideDir)
    {
        switch (slideDir)
        {
            case ("right"):

                //Setup and slide pages to the left (Page Number -1)

                break;

            case ("left"):

                //Setup and slide pages to the right (Page Number +1)
                //Move the Master Panel to 0,0, to prepare for a Left slide animation
                ScrapPanel.transform.position = new Vector3(976, 0, 0);
                //LivePanel.transform.position = Vector3.zero;

                //Reach into the Content, and trigger the Left Slide animation
                var scrapanim = ScrapPanel.transform.GetChild(0).GetComponent<Animator>();
                scrapanim.SetTrigger("slideLeft");

                var liveanim = LivePanel.transform.GetChild(0).GetComponent<Animator>();
                liveanim.SetTrigger("slideLeft");

                //Move the Master Panel to the extents of the animation
                ScrapPanel.transform.position = new Vector3(0, 0, 0);
                LivePanel.transform.position = new Vector3(-976, 0, 0);

                //Reach into the Content, and trigger the Reset
                scrapanim.SetTrigger("Reset");
                liveanim.SetTrigger("Reset");

                //Clear the Content

                break;

            default:
                break;
        }

        //Flip the Live and Scrap Panel roles
        if (LivePanel == PanelA)
        {
            LivePanel = PanelB;
        }
        else
        {
            LivePanel = PanelA;
        }

        if (ScrapPanel == PanelB)
        {
            ScrapPanel = PanelA;
        }
        else
        {
            ScrapPanel = PanelB;
        }

    }

    public void SlideRight()
    {
        //Increment the page number by 1
        nextPageNum = currentPage + 1;

        //Tell the SlidePages which way to slide
        slideDirection = "left";

        //We need to clear the contents
        ClearPages();

        //Fill the pages with the new content
        FillPages();

        //Call SlidePages and send it the direction that you want to slide
        SlidePages(slideDirection);
    }
}
