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

public class RatingPanelController : PanelController
{
    [SerializeField] private TMP_Text getPointsText;
    [SerializeField] private GameObject threePointsIndicatorGameObject;
    [SerializeField] private GameObject fivePointsIndicatorGameObject;
    [SerializeField] private GameObject tenPointsIndicatorGameObject;

    private Enums.GameResult _gameResult;
    private int _oldScore;
    private int _newScore;
    private int _myRating;
    private RatingPointsController _ratingPointsController;
        
    public void Show(Enums.GameResult gameResult)
    {
        base.Show(InitRatingPanel(gameResult));
    }

    public void OnClickConfirmButton()
    {
        Hide();
    }

    public void OnClickRetryButton()
    {
        Hide(() => { });
    }
    
    
    /// <summary>
    /// 텍스트 초기화, 승급포인트 계산
    /// </summary>
    /// <param name="isWin"></param>
    public PanelControllerShowDelegate InitRatingPanel(Enums.GameResult gameResult)
    {
        _gameResult = gameResult; 
        _myRating= UserManager.Instance.Rating;
        int requiredScore = 0;
        if (_myRating >= 10 && _myRating <= 18) // 10~18급은 3점 필요
        {
            requiredScore = 3;
            threePointsIndicatorGameObject.SetActive(true);
            _ratingPointsController = threePointsIndicatorGameObject.GetComponent<RatingPointsController>();
            
        } 
        else if (_myRating >= 5 && _myRating <= 9) // 5~9급은 5점 필요
        {
            requiredScore = 5;
            fivePointsIndicatorGameObject.SetActive(true);
            _ratingPointsController = fivePointsIndicatorGameObject.GetComponent<RatingPointsController>();
        } 
        else if (_myRating >= 1 && _myRating <= 4) // 1~4급은 10점 필요
        {
            requiredScore = 10;
            tenPointsIndicatorGameObject.SetActive(true);
            _ratingPointsController = tenPointsIndicatorGameObject.GetComponent<RatingPointsController>();
        }

        string win = _gameResult == Enums.GameResult.Win ? "승리" : "패배";
        string get = _gameResult == Enums.GameResult.Win  ? "얻었습니다." : "잃었습니다.";
        
        // 게임 전 스코어로 초기화
        NetworkManager.Instance.GetInfo((userInfo) =>
            {
                _oldScore = userInfo.score;
                // TODO: oldscore가 더이상 강등될 수 없는 상태, 더이상 승급할 수 없는 상태임을 체크해서 더이상 ~할 수 없습니다. 처리하기
                if (_myRating == 1 && userInfo.score >= requiredScore )
                {
                    
                }
                else if (_myRating == 18 && userInfo.score <= requiredScore*2)
                {
                    
                }
                else
                {
                    _ratingPointsController.InitRatingPoints(_oldScore,_gameResult,requiredScore);
                    
                }
            }, () =>
            { });

        if(_gameResult == Enums.GameResult.Draw)
        {
            getPointsText.text = "무승부입니다.";
        }
        else
        {
            getPointsText.text = $"게임에서 {win}했습니다.\n{Constants.RAING_POINTS} 승급 포인트를 {get}";
        }

        return null;
    }
    

}