using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.EventSystems;

public class MainTestBinaryPack : MonoBehaviour
{
    static bool initialized = SerializeStrVector3.Initialize();
    BinaryPack binaryPack;
    const string filePath = "Serialize.bin";
    

    GameObject Cube1, Cube2;
    GameObject moveTarget = null;// hbNΏۂ̋LpihbNȊOnullj
    Vector3 prevMousePosition;// hbNړÕ}EX[hW

    void Start()
    {
        SerializeStrVector3.Initialize();

        // IɂQ̃L[u𐶐
        Cube1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
        Cube1.name = "Cube1";
        Cube1.transform.position = new Vector3(-5, 3, 0);

        Cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
        Cube2.name = "Cube2";
        Cube2.transform.position = new Vector3(5, -3, 0);

        // filePath̃t@C݂΁Ãt@CeŁAʒu𕜌B
        FileInfo fileInfo = new FileInfo(filePath);
        if ( true && fileInfo.Exists)
        {
            byte[] buff = new byte[fileInfo.Length];
            using (FileStream stream = fileInfo.OpenRead())// t@CẽoCibuffɋL
            {
                int n = 0;
                do { n += stream.Read(buff, n, buff.Length); 
                } while (n < buff.Length);
            }
            binaryPack = new BinaryPack();// eXgΏۂ̃VACYIuWFNgp
            int idx = 0;
            SerializeStrVector3 ssv3;// eIuWFNg̕p
            while(idx < buff.Length)// ʒu(Vector3)𕜌Đݒ肷JԂ
            {
                ssv3 = (SerializeStrVector3)BinaryPack.GetDeserializedObject(buff, ref idx);
                GameObject obj = GameObject.Find(ssv3.str);//Ώۂ̃IuWFNg擾
                obj.transform.position = ssv3.v3;
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Vector3 mouseScreenPosition = Input.mousePosition;
            mouseScreenPosition.z = -Camera.main.transform.position.z;// ZC
            Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(mouseScreenPosition);
            Ray ray = Camera.main.ScreenPointToRay(mouseScreenPosition);
            RaycastHit hit;
            if ( Physics.Raycast(ray, out hit) == false && moveTarget == null) return;// RayǂɂȂ
            if(moveTarget == null)
            {
                if (hit.collider.gameObject.name == "Cube1") moveTarget = Cube1;
                if (hit.collider.gameObject.name == "Cube2") moveTarget = Cube2;
                prevMousePosition = mouseWorldPosition;
            }
            else
            {
                Vector3 move = mouseWorldPosition - prevMousePosition;//}EXhbN
                prevMousePosition = mouseWorldPosition;
                moveTarget.transform.position += move;
                Debug.Log($"{mouseScreenPosition}---> {mouseWorldPosition}");
            }
        }
        else
        {
            moveTarget = null;
        }
    }

    public void OnApplicationQuit()// vOIɁASerializeStrVector3IuWFNgt@C
    {
        binaryPack = new BinaryPack();
        SerializeStrVector3 ssv3 = new SerializeStrVector3();
        ssv3.str = "Cube1";
        ssv3.v3 = this.Cube1.transform.position;
        binaryPack.Add(SerializeStrVector3.getBytesByMyData(ssv3));
        ssv3.str = "Cube2";
        ssv3.v3 = this.Cube2.transform.position;
        binaryPack.Add(SerializeStrVector3.getBytesByMyData(ssv3));
        byte[] buff = binaryPack.GetSerializedBuffer();// LQVACYoCg擾
        using (FileStream stream = File.Open(filePath, FileMode.Create))
        {
            stream.Write(buff, 0, buff.Length);// t@Cɏ
        }
    }
}

