using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class MainManager : MonoBehaviour {
    // プレイヤー格納
    [SerializeField]
    GameObject Player;
    // 救助するキャラ格納
    [SerializeField]
    GameObject Peoples;
    // サメ格納
    [SerializeField]
    GameObject Enemy;

    [SerializeField]
    GameObject HealtObj;
    [SerializeField]
    TextMesh ScoreText;

    [SerializeField]
    GameObject FadeObj;

    [SerializeField]
    GameObject DamageFade;

    [SerializeField]
    Sprite People2Image;
    [SerializeField]
    Sprite Enemy2Image;

    int SceneChange = 0;

    // Use this for initialization
    void Start() {
        PlayerYBase = Player.transform.position.y;
        Healt = MaxHealt;
        HealtObj.GetComponent<Healt>().Startini(Healt);
        ScoreText.text = "あと" + MaxScore + "ポイント";
    }
    // 体力
    [SerializeField]
    int MaxHealt = 9;
    int Healt = 9;
    // 無敵時間
    float Muteki = 0;
    // プレイヤーのY座標基準
    float PlayerYBase;
    // プレイヤーの加速度
    float PlayerSpeedPlus = 1.0f;
    // プレイヤーの最大速度
    float PlayerSpeedMax = 8.0f;
    // プレイヤーの現在速度
    float PlayerSpeed = 0.0f;
    float PlayerSpeedY = 0.0f;
    // プレイヤーの位置
    float PlayerPosX = 0.0f;
    float PlayerPosY = 0.0f;
    // プレイヤー
    void Update() {
        switch (SceneChange) {
            case 0:
                // 無敵処理
                if (Muteki > 0) {
                    Muteki -= Time.deltaTime;
                    if (Muteki < 1) {
                        if (Muteki % 0.1 < 0.05f) {
                            Player.GetComponent<ColorFade>().ColorChange(Color.red);
                        } else {
                            Player.GetComponent<ColorFade>().ColorChange(new Color(1.0f, 1.0f, 1.0f, 1.0f));
                        }
                    } else {
                        if (Muteki % 0.25 < 0.125f) {
                            Player.GetComponent<ColorFade>().ColorChange(Color.yellow);
                        } else {
                            Player.GetComponent<ColorFade>().ColorChange(new Color(1.0f, 1.0f, 1.0f, 1.0f));
                        }
                    }
                } else {
                    Player.GetComponent<ColorFade>().ColorChange(new Color(1.0f, 1.0f, 1.0f, 1.0f));
                }
                int PlayerButton = 0;
                if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) {
                    PlayerButton--;
                }
                if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) {
                    PlayerButton++;
                }
                // プレイヤー操作による加速
                if (PlayerSpeed > 0 && PlayerButton < 0) {
                    PlayerSpeed += (float)PlayerButton * PlayerSpeedPlus * 2.0f;
                } else if (PlayerSpeed < 0 && PlayerButton > 0) {
                    PlayerSpeed += (float)PlayerButton * PlayerSpeedPlus * 2.0f;
                } else {
                    PlayerSpeed += (float)PlayerButton * PlayerSpeedPlus;
                }
                // 波で後ろに持っていかれる
                PlayerSpeed -= 0.1f;
                PlayerSpeed = Mathf.Clamp(PlayerSpeed, -PlayerSpeedMax - 0.5f, PlayerSpeedMax);
                // プレイヤーの位置を適用する
                PlayerPosX += PlayerSpeed * Time.deltaTime;
                if (PlayerPosX < -7.5f) {
                    // 後ろに下がりすぎた場合は加速度を0にして位置も訂正
                    PlayerSpeed = 0.0f;
                    PlayerPosX = -7.5f;
                }
                if (PlayerPosX > 7.5f) {
                    // 前に出すぎた場合は加速度を0にして位置も訂正
                    PlayerSpeed = 0.0f;
                    PlayerPosX = 7.5f;
                }

                // プレイヤーのX座標を決定
                Player.transform.position = new Vector3(
PlayerPosX,
GetComponent<PlayerMove>().YPosition(PlayerPosX) + PlayerYBase, 0);

                // プレイヤーのY座標関係
                PlayerButton = 0;
                if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W)) {
                    PlayerButton++;
                }
                if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) {
                    PlayerButton--;
                }
                // プレイヤー操作による加速
                PlayerSpeedY += (float)PlayerButton * PlayerSpeedPlus;
                // 0スピードに強い力で引っ張られる
                if (PlayerSpeedY > 0) {
                    PlayerSpeedY = Mathf.Max(0.0f, PlayerSpeedY - 0.2f);
                } else {
                    PlayerSpeedY = Mathf.Min(0.0f, PlayerSpeedY + 0.2f);
                }
                PlayerSpeedY = Mathf.Clamp(PlayerSpeedY, -PlayerSpeedMax, PlayerSpeedMax);
                // プレイヤーの位置を適用する
                PlayerPosY += PlayerSpeedY * Time.deltaTime;
                if (PlayerPosY < -2.0f) {
                    // 左すぎの場合は加速度を0にして位置も訂正
                    PlayerSpeedY = 0.0f;
                    PlayerPosY = -2.0f;
                }
                if (PlayerPosY > 2.0f) {
                    // 右すぎの場合は加速度を0にして位置も訂正
                    PlayerSpeedY = 0.0f;
                    PlayerPosY = 2.0f;
                }
                // プレイヤーのY座標を決定
                Player.transform.position += new Vector3(
0.0f, PlayerPosY,
PlayerPosY * 0.01f);


                // 特定の確率で被災者が流れてくる
                if (UnityEngine.Random.Range(0, 100) < 3) {
                    GameObject obj = Instantiate(Peoples) as GameObject;
                    if (UnityEngine.Random.Range(0, 10) < 3) {
                        obj.GetComponent<SpriteRenderer>().sprite = People2Image;
                    }
                    obj.transform.position = new Vector3(10, 0, 0);
                    obj.GetComponent<PeoPlePoint>().XMoveSpeed = UnityEngine.Random.Range(3.0f, 8.0f);
                    obj.GetComponent<PeoPlePoint>().YPos = UnityEngine.Random.Range(0f, -4f);
                    obj.GetComponent<PeoPlePoint>().Point = UnityEngine.Random.Range(80, 200);
                    obj.GetComponent<PeoPlePoint>().Manager = gameObject;
                }
                break;
            case 5:
                GetComponent<await>().start(2.0f);
                FadeObj.GetComponent<ColorFade>().ColorChange(new Color(1.0f, 1.0f, 1.0f, 1.0f), 2.0f);
                SceneChange++;
                break;
            case 6:
                if (GetComponent<await>().await_finish()) {
                    GameOver_Manager.ClearFlag = true;
                    SceneManager.LoadScene("GameOver");
                }
                break;
            case 10:
                GetComponent<await>().start(2.0f);
                FadeObj.GetComponent<ColorFade>().ColorChange(new Color(0.0f, 0.0f, 0.0f, 1.0f), 2.0f);
                SceneChange++;
                break;
            case 11:
                if (GetComponent<await>().await_finish()) {
                    GameOver_Manager.ClearFlag = false;
                    SceneManager.LoadScene("GameOver");
                }
                break;
        }
        if (SceneChange < 3 || SceneChange > 9) {
            // 特定の確率でサメが襲ってくる
            SameRandom--;
            SameRand -= Time.deltaTime / 2;
            if (SameRand < 20) {
                SameSpeedOffset = 50;
                SameRand = 0;
            }
            int LoopCnt = 1;
            if (SameSpeedOffset == 50) LoopCnt = 5;
            for (int i = 0; i < LoopCnt; i++) {
                if (UnityEngine.Random.Range(0, SameRandom) < 1) {
                    SameRandom += Mathf.RoundToInt(SameRand);
                    GameObject obj = Instantiate(Enemy) as GameObject;
                    if (UnityEngine.Random.Range(0, 10) < 3) {
                        obj.GetComponent<SpriteRenderer>().sprite = Enemy2Image;
                    }
                    obj.transform.position = new Vector3(10, 0, 0);
                    obj.GetComponent<Enemy>().XMoveSpeed = UnityEngine.Random.Range(SameSpeedOffset + 8.0f, SameSpeedOffset + 12.0f);
                    obj.GetComponent<Enemy>().YPos = UnityEngine.Random.Range(0f, -4f);
                    obj.GetComponent<Enemy>().Manager = gameObject;

                }
            }
        }
    }
        
    // サメの乱数上限制限
    int SameRandom = 100;
    // サメの確率制御
    float SameRand = 50.0f;
    // サメ速度オフセット
    float SameSpeedOffset = 0.0f;
    public void Damage() {
        if (SceneChange == 0) {
            if (Muteki <= 0) {
                GetComponent<SEPlay>().SePlay(2);
                DamageFade.GetComponent<SpriteRenderer>().color += new Color(0.0f, 0.0f, 0.0f, 0.8f);
                DamageFade.GetComponent<ColorFade>().Fadeout(0.2f);
                Healt--;
                HealtObj.GetComponent<Healt>().HPUpdate(Healt);
                Muteki = 2;
                if (Healt <= 0) {
                    SameRand = 0;
                    SceneChange = 10;
                }
            }
        }
    }
    [SerializeField]
    int MaxScore = 4000;
    int score = 0;
    // スコア追加
    public void ScoreAdd(int i) {
        GetComponent<SEPlay>().SePlay(1);
        if (SceneChange == 0) {
            score += i;
            if(score >= MaxScore) {
                ScoreText.text = "ゲームクリア";
                SceneChange = 5;
                return;
            }
            ScoreText.text = "あと" + (MaxScore - score) + "ポイント";
        }
    }
}
