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

public class Manager : MonoBehaviour
{
  public Material LightOn;
  public Material LightOff;
  public Material FreqMarkOn;
  public Material FreqMarkOff;
  public Material PanelOn;
  public Material PanelOff;
  public Transform FrequencyMarker;
  public RotateKnob Frequency;
  public RotateKnob Antenna;
  public StepKnob AMFM;
  public RotateKnob Volume;

  public float FrequencyValue;
  public float AntennaValue;
  public float VolumeValue;
  public float AMFMValue;

  public float CorrectFrequency;
  public int CorrectAMFM;
  public float CorrecAntenna;

  public static string Locked = "";

  bool _radioOn;

  AudioSource _lampAudio;

  AudioMixer _mixer;
  void Start()
  {
    Frequency = GameObject.Find("FrequencyKnob").GetComponent<RotateKnob>();
    Antenna = GameObject.Find("Antenna").GetComponent<RotateKnob>();
    Volume = GameObject.Find("VolumeKnob").GetComponent<RotateKnob>();
    AMFM = GameObject.Find("FMAMKnob").GetComponent<StepKnob>();

    _mixer = Resources.Load("Mixer") as AudioMixer;

    SetCorrectValues();

    _lampAudio = GetComponent<AudioSource>();

  }

  void GetValues()
  {
    FrequencyValue = Map(Frequency.Value, Frequency.ValueMin, Frequency.ValueMax, 0, 100);
    AntennaValue = Map(Antenna.Value, Antenna.ValueMin, Antenna.ValueMax, 0, 100);
    VolumeValue = Map(Volume.Value, Volume.ValueMin, Volume.ValueMax, 0, 100);
    AMFMValue = AMFM.Value;
  }
  void SetCorrectValues()
  {
    CorrectFrequency = Random.Range(1f, 99f);
    CorrecAntenna = Random.Range(1f, 99f);
    CorrectAMFM = Random.Range(0, 3);
  }

  void SetNoises()
  {
    var freq = Mathf.Abs(FrequencyValue - CorrectFrequency);
    var antenna = Mathf.Abs(AntennaValue - CorrecAntenna);
    var amfm = CorrectAMFM == AMFMValue;

    float n1val;
    float n2val;
    float n3val;
    float sval;

    _mixer.GetFloat("Noise1", out n1val);
    _mixer.GetFloat("Noise2", out n2val);
    _mixer.GetFloat("Noise3", out n3val);
    _mixer.GetFloat("Sine", out sval);

    if (freq > 7 && n1val < -9)
    {
      _mixer.SetFloat("Noise1", -7);
    }
    if (freq < 3.5)
    {
      _mixer.SetFloat("Noise1", -17);
    }
    if (freq < 1.5f)
    {
      _mixer.SetFloat("Noise1", -25);
    }

    if (antenna > 10 && n2val < -8)
    {
      _mixer.SetFloat("Noise2", -5);
    }
    if (antenna < 7)
    {
      _mixer.SetFloat("Noise2", -8);
    }
    if (antenna < 4)
    {
      _mixer.SetFloat("Noise2", -16);
    }
    if (antenna < 2.5)
    {
      _mixer.SetFloat("Noise2", -26);
    }

    if (amfm)
    {

    }
    else
    {
      if ((AMFMValue == 0 && CorrectAMFM == 1)
          || (AMFMValue == 1 && CorrectAMFM == 2)
          || (AMFMValue == 2 && CorrectAMFM == 0)
      )
      {
        _mixer.SetFloat("Noise3", 0);
      }
      else
      {
        _mixer.SetFloat("Noise3", 10);
      }
    }

    if (Input.GetMouseButton(0))
    {
      _mixer.SetFloat("Sine", -20);
    }
    else if (!Input.GetMouseButton(0) && sval > -27)
    {
      _mixer.SetFloat("Sine", -35);
    }

  }

  bool CheckFrequency()
  {
    var delta = Mathf.Abs(FrequencyValue - CorrectFrequency);
    return delta < .5f;
  }
  bool CheckAntenna()
  {
    var delta = Mathf.Abs(AntennaValue - CorrecAntenna);
    return delta < 1.5f;
  }
  bool CheckAMFM()
  {
    return AMFMValue == CorrectAMFM;
  }

  void Update()
  {
    if (Input.GetKeyDown(KeyCode.Escape))
    {
      Application.Quit();
    }
    GetValues();

    var freqPos = FrequencyMarker.position;
    freqPos.x = Map(FrequencyValue, 0, 100, -1.2f, 2.25f);
    FrequencyMarker.position = freqPos;

    _mixer.SetFloat("Volume", Mathf.Clamp(Map(VolumeValue, 0, 80, -50, 5), -60, 5));
    if (!_radioOn && VolumeValue > 5)
    {
      GameObject.Find("Lamp").GetComponent<MeshRenderer>().material = LightOn;
      GameObject.Find("FrequencyMarker").GetComponent<MeshRenderer>().material = FreqMarkOn;
      GameObject.Find("DisplayPanel").GetComponent<MeshRenderer>().material = PanelOn;
      _radioOn = true;
      _lampAudio.Play();
    }
    else if (_radioOn && VolumeValue < 4.9)
    {
      GameObject.Find("Lamp").GetComponent<MeshRenderer>().material = LightOff;
      GameObject.Find("FrequencyMarker").GetComponent<MeshRenderer>().material = FreqMarkOff;
      GameObject.Find("DisplayPanel").GetComponent<MeshRenderer>().material = PanelOff;
      _radioOn = false;
      _lampAudio.Play();
    }
    if (!_radioOn)
    {
      _mixer.SetFloat("Volume", -80);
    }
    SetNoises();
    var freq = CheckFrequency();
    var ant = CheckAntenna();
    var fm = CheckAMFM();
    if (freq)
    {
      _mixer.SetFloat("Noise1", -80);
    }
    if (ant)
    {
      _mixer.SetFloat("Noise2", -80);
    }
    if (fm)
    {

    }
    if (ant && freq && fm)
    {
      _mixer.SetFloat("Meri", 5);
      _mixer.SetFloat("Sine", -80);
      _mixer.SetFloat("Noise3", -80);
      _mixer.SetFloat("Hitler", -80);
    }
    else if (ant && freq)
    {
      if ((AMFMValue == 0 && CorrectAMFM == 1)
          || (AMFMValue == 1 && CorrectAMFM == 2)
          || (AMFMValue == 2 && CorrectAMFM == 0)
      )
      {
        _mixer.SetFloat("Hitler", -5);
      }
      _mixer.SetFloat("Meri", -80);
    }
    else
    {
      _mixer.SetFloat("Hitler", -80);
      _mixer.SetFloat("Meri", -80);
    }
    Cursor.visible = Locked == "";
  }

  float Map(float s, float a1, float a2, float b1, float b2)
  {
    return b1 + (s - a1) * (b2 - b1) / (a2 - a1);
  }
}
