using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour {

    [SerializeField]
    AbilityType type;

    public float damage = 1;

    [SerializeField]
    Vector2 direction;

    [SerializeField]
    float destroyTime = 10f;

    [SerializeField]
    float speed;

    private Rigidbody2D rb;
    SpriteRenderer spriteRenderer;

    GameObject player;

    public AbilityType Type {
        get {
            return type;
        }
        set {
            type = value;
        }
    }

    void Awake() {
        rb = GetComponent<Rigidbody2D>();
        player = GameObject.FindGameObjectWithTag("Player").transform.GetChild(0).gameObject;
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    bool plus = false;
    Vector3 mouse;



    float mx, my;
    float x, x2, y, y2;

    

    void Start() {
        
        plus = player.transform.localScale.x > 0;
        mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);


        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; //sub'ing pos of player from the mouse pos
        difference.Normalize(); //making all values (sum of vector) equal to 1

        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; //finding angle between mouse and x axis
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + 0);



        if(!plus) {
            
            transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
            transform.position = new Vector3(transform.position.x- (GetComponent<SpriteRenderer>().sprite.bounds.size.x*2.6f), transform.position.y, transform.position.z);

            //foreach(var sr in player.GetComponentsInChildren<SpriteRenderer>()) {
            //    sr.transform.localScale = new Vector3(-sr.transform.localScale.x, sr.transform.localScale.y, sr.transform.localScale.z);
            //}
        }

        //transform.position = new Vector3(transform.localScale.x * direction.x, transform.localScale.y, transform.localScale.z);

        switch(type) {
            case AbilityType.Electricity:
                speed = .65f;
                break;
            case AbilityType.Sound:
                speed = 0.532f;
                break;
            case AbilityType.Light:
                speed = .185f;
                break;
            default:
                break;
        }


        //DEBUGGGGGGGGGGGGGGGGGGG
        speed *= 20;





        mx = mouse.x - transform.position.x;
        my = mouse.y - transform.position.y;



        Destroy(gameObject, destroyTime);
    }


    void Update() {

        Vector3 newPoint = new Vector3((mouse.x + mx) * 50, (mouse.y + my) * 50, 0);


        float step = speed * Time.deltaTime;

        transform.position = Vector3.MoveTowards(transform.position, newPoint, step);

        //transform.position = new Vector3(plus ? transform.position.x + (1.0f / speed) * Time.deltaTime : transform.position.x - (1.0f / speed) * Time.deltaTime,  transform.position.y, transform.position.z);
        //spriteRenderer.flipY = !spriteRenderer.flipY;

    }

    void OnCollisionEnter2D(Collision2D col) {
        Debug.Log("Bullet collided with something2d" + col.transform.name);
        

    }



}
