using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

[RequireComponent(typeof(CircleCollider2D))]
[RequireComponent(typeof(Rigidbody2D))]
public class Fruit : MonoBehaviour
{
    private Rigidbody2D rig2D;

    public void OnInit() 
    {
        rig2D = GetComponent<Rigidbody2D>();
        rig2D.bodyType = RigidbodyType2D.Kinematic;
    }

    public void OnDrag() 
    {
        rig2D.bodyType = RigidbodyType2D.Dynamic;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        //如果两个水果名字一样
        if(gameObject.name == collision.gameObject.name) 
        {
            //如果另一边的水果已经触发 碰撞函数 则直接返回
            if (GameController.GetInstance().TempList.Contains(gameObject))
            {
                GameController.GetInstance().TempList.Remove(gameObject);
                return;
            }

            //把被碰撞的水果 放入集合中 防止重复碰撞检测
            GameController.GetInstance().TempList.AddLast(collision.gameObject);

            //两个水果的中间位置
            Vector3 MidPos = (transform.position + collision.transform.position) / 2;
            //生成一个新的
            string NewObjName = (int.Parse(gameObject.name) + 1).ToString();
            GameObject Obj = Instantiate(Resources.Load<GameObject>("Fruits/" + NewObjName), MidPos, Quaternion.identity);
            Obj.transform.localScale = new Vector3(0.3f, 0.3f, 1);
            Obj.name = NewObjName;

            //取两个水果中 速度大的一个 赋值给新生成的水果
            Vector2 Velocity = 
                gameObject.GetComponent<Rigidbody2D>().velocity.magnitude > collision.gameObject.GetComponent<Rigidbody2D>().velocity.magnitude?
                gameObject.GetComponent<Rigidbody2D>().velocity : collision.gameObject.GetComponent<Rigidbody2D>().velocity;

            Obj.GetComponent<Rigidbody2D>().velocity = Velocity;

            #region 上报水果合成

            Dictionary<string, object> paras = new Dictionary<string, object>();
            paras.Add("type", int.Parse(gameObject.name));
            HttpTool.Instance._Get("app/v1/watermelon/synthetic", paras, new Action<List<int>>((result) =>
            {

            }), new Action<string, string>((code, msg) =>
            {
                Debug.unityLogger.Log("合成水果上报 " + code + " msg " + msg);
            }));

            #endregion

            //生成爆炸效果
            Instantiate(Resources.Load<GameObject>("Prefabs/BoomEffect"), MidPos, Quaternion.identity);

            //生成音符特效
            GameObject obj = ResMgr.Getinstance().Load<GameObject>("Prefabs/Note");
            obj.transform.ResetToCanvas(E_Layer.system,UIMgr.Getinstance().canvas.InverseTransformPoint(MidPos));
            obj.GetComponent<Image>().sprite = 
                ResMgr.Getinstance().Load<SpriteAtlas>("Atlas/Notes").GetSprite("note" + UnityEngine.Random.Range(1, 4).ToString());
            (obj.transform as RectTransform).DoMove(0.5f, new Vector2(0, 580),trans=> { Destroy(trans.gameObject); });

            //发送事件
            EventCenter.Getinstance().EventTrigger("FlyNote");

            Destroy(collision.gameObject);
            Destroy(this.gameObject);
        }
    }
}
