using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI;

/// <summary>
/// Проектирование помещения
/// </summary>
public class LocationController : MonoBehaviour
{
    public List<GameObject> rooms;
    public GameObject Location;
    // Start is called before the first frame update
    void Start()
    {
        // location_22
        //var rooms_0 = rooms.ElementAt(0);
        //Cube(rooms_0, new Vector3(6, 8, 1)); //x=6 y=8 h1
        //var rooms_1 = rooms.ElementAt(1);
        //Cube(rooms_1, new Vector3(2, 10, 1), new Vector2(6, 0));
        //var rooms_2 = rooms.ElementAt(2);
        //Cube(rooms_2, new Vector3(5, 4, 1), new Vector2(8, 5));

        // location_25
        //var rooms_0 = Instantiate(Resources.Load("GameObjects/Cube", typeof(GameObject)) as GameObject);
        //rooms_0.transform.SetParent(Location.transform);
        //Cube(rooms_0, new Vector3(3.57f, 6.31f, 1)); //x=6 y=8 h1
        //rooms.Add(rooms_0);
        //var rooms_1 = Instantiate(Resources.Load("GameObjects/Cube", typeof(GameObject)) as GameObject);
        //rooms_1.transform.SetParent(Location.transform);
        //Cube(rooms_1, new Vector3(4, 3.7f, 1), new Vector2(3.57f, 0)); //x=6 y=8 h1
        //rooms.Add(rooms_1);
        //var rooms_2 = Instantiate(Resources.Load("GameObjects/Cube", typeof(GameObject)) as GameObject);
        //rooms_2.transform.SetParent(Location.transform);
        //Cube(rooms_2, new Vector3(3, 4.46f, 1), new Vector2(3.57f, 1.85f)); //x=6 y=8 h1
        //rooms.Add(rooms_2);

        // location_1
        var rooms_0 = Instantiate(Resources.Load("GameObjects/Cube", typeof(GameObject)) as GameObject);
        rooms_0.transform.SetParent(Location.transform);
        Cube(rooms_0, new Vector3(22.8f, 5.5f, 1)); //x=6 y=8 h1
        rooms.Add(rooms_0);

        //var rooms_1 = rooms.ElementAt(1);
        //Cube(rooms_1, new Vector3(2, 10, 1), new Vector2(6, 0));
        //var rooms_2 = rooms.ElementAt(2);
        //Cube(rooms_2, new Vector3(5, 4, 1), new Vector2(8, 5));

        //Room("r0", new Vector3(6, 8, 1)); //x=6 y=8 h1
        //Room("r1", new Vector3(2, 10, 1), new Vector2(6, 0));
        //Room("r2", new Vector3(5, 4, 1), new Vector2(8, 5));
    }

    // Update is called once per frame
    void Update()
    {

    }



    public static void Cube(GameObject cube, Vector3 cube_size, Vector2 position = new Vector2(), float wall_thickness = 0.01f)
    {
        //GameObject cube = GameObject.Find("Cube");
        cube_size = new Vector3(cube_size.x, cube_size.y, cube_size.z);

        Vector3 pos_cube = new Vector3(cube_size.x / 2f, cube_size.z / 2f, cube_size.y / 2f);
        cube.transform.position = new Vector3(pos_cube.x + position.x, pos_cube.y, pos_cube.z + position.y);

        // above
        GameObject plane_a = cube.transform.GetChild(0).gameObject;
        //plane_above.transform.localScale = cube_size;
        plane_a.transform.localScale = new Vector3(cube_size.x, wall_thickness, cube_size.y);
        plane_a.transform.localPosition = new Vector3(0, pos_cube.y, 0);//y x=0

        // front
        GameObject plane_b = cube.transform.GetChild(1).gameObject;
        plane_b.transform.localScale = new Vector3(cube_size.x, wall_thickness, cube_size.z);    
        plane_b.transform.localPosition = new Vector3(0, 0, -pos_cube.z);

        // side
        GameObject plane_c = cube.transform.GetChild(2).gameObject;
        plane_c.transform.localScale = new Vector3(cube_size.y, wall_thickness, cube_size.z);
        plane_c.transform.localPosition = new Vector3(-pos_cube.x, 0, 0);

        // низ 
        GameObject plane_d = cube.transform.GetChild(3).gameObject;
        plane_d.transform.localScale = new Vector3(cube_size.x, wall_thickness, cube_size.y);
        plane_d.transform.localPosition = new Vector3(0, -pos_cube.y, 0);

        // зад 
        GameObject plane_e = cube.transform.GetChild(4).gameObject;
        plane_e.transform.localScale = new Vector3(cube_size.y, wall_thickness, cube_size.z);
        plane_e.transform.localPosition = new Vector3(pos_cube.x, 0, 0);

        // front 2
        GameObject plane_f = cube.transform.GetChild(5).gameObject;
        plane_f.transform.localScale = new Vector3(cube_size.x, wall_thickness, cube_size.z);
        plane_f.transform.localPosition = new Vector3(0, 0, pos_cube.z);
    }

    public static void Room(string room_name, Vector3 cube_size, Vector2 position = new Vector2())
    {
        //GameObject cube = GameObject.Find("Cube");
        var room = Instantiate(Resources.Load("GameObjects/RoomTest", typeof(GameObject)) as GameObject);
        room.name = room_name;
        cube_size = new Vector3(cube_size.x, cube_size.y, cube_size.z);

        Vector3 pos_cube = new Vector3(cube_size.x / 2f, cube_size.z / 2f, cube_size.y / 2f);
        room.transform.position = new Vector3(pos_cube.x + position.x, pos_cube.y, pos_cube.z + position.y);

        // above
        var SideA = room.transform.GetChild(0).gameObject;
        var panel_a = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject)) as GameObject);
        panel_a.name = "Plane_a_" + 0;
        panel_a.transform.SetParent(SideA.transform);       
        //GameObject plane_a = cube.transform.GetChild(0).gameObject;
        //plane_above.transform.localScale = cube_size;
        panel_a.transform.localScale = new Vector3(cube_size.x, 0.01f, cube_size.y);
        panel_a.transform.localPosition = new Vector3(0, pos_cube.y, 0);//y x=0
        panel_a.transform.rotation = SideA.transform.rotation;

        // front
        var SideB = room.transform.GetChild(1).gameObject;
        //GameObject plane_b = cube.transform.GetChild(1).gameObject;
        //plane_b.transform.localScale = new Vector3(cube_size.x, 0.01f, cube_size.z);
        for (int i = 0; i < cube_size.x; i++)
        {
            var panel = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject;
            panel.name = "Plane_b_" + i;
            panel.transform.SetParent(SideB.transform);
            panel.transform.localPosition = new Vector3(i, 0, -pos_cube.z);
            panel.transform.rotation = SideB.transform.rotation;
        }

        // side
        var SideC = room.transform.GetChild(2).gameObject;
        //GameObject plane_c = cube.transform.GetChild(2).gameObject;
        for (int i = 0; i > -cube_size.x; i--)
        {
            var panel = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject;
            panel.name = "Plane_c_" + i;
            panel.transform.SetParent(SideC.transform);
            panel.transform.position = new Vector3(i, 0, 0);
            panel.transform.rotation = SideC.transform.rotation;
        }

        // низ 
        var SideD = room.transform.GetChild(3).gameObject;

        var panel_d = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject;
        //GameObject plane_d = cube.transform.GetChild(3).gameObject;
        panel_d.name = "Plane_d_" + 0;
        panel_d.transform.SetParent(SideD.transform);
        panel_d.transform.localScale = new Vector3(cube_size.x, 0.01f, cube_size.y);
        panel_d.transform.position = new Vector3(0, -pos_cube.y, 0);
        panel_d.transform.rotation = SideD.transform.rotation;


        // зад 
        var SideE = room.transform.GetChild(4).gameObject;
        for (int i = 0; i > -cube_size.x; i--)
        {
            var panel = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject;
            //GameObject plane_e = cube.transform.GetChild(4).gameObject;
            panel.name = "Plane_e_" + i;
            panel.transform.SetParent(SideE.transform);
            panel.transform.position = new Vector3(pos_cube.x, 0, 0);
            panel.transform.rotation = SideE.transform.rotation;
        }

        // front 2
        var SideF = room.transform.GetChild(5).gameObject;
        for (int i = 0; i > -cube_size.x; i--)
        {
            var panel = Instantiate(Resources.Load("GameObjects/Panel", typeof(GameObject))) as GameObject;
            //GameObject plane_f = cube.transform.GetChild(5).gameObject;
            panel.name = "Plane_f_" + i;
            panel.transform.SetParent(SideF.transform);
            panel.transform.position = new Vector3(0, 0, pos_cube.z);
            panel.transform.rotation = SideF.transform.rotation;
        }
    }    
}