using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Codelock : MonoBehaviour
{
    
    [Header("---------- ГЕНЕРАЦИЯ ПАРОЛЯ ----------")]
    public Codelock.password_element[] _Codelock_password_element_AR;
    [HideInInspector] public int[] number_AR;
    private char[] password_chars_AR;
    [HideInInspector] public string password;
    public Material[] number_materials_AR;
    public string very_simple_password_0 = "0000";
    public string very_simple_password_1 = "1111";
    public string norm_password = "6589";
    [Header("---------- ВВОД И ПРОВЕРКА ПАРОЛЯ ----------")]
    public Transform camera_TR;
    private RaycastHit hit;
    public AudioSource button_sound;
    public AudioSource lock_open_sound;
    public AudioSource wrong_code_sound;
    public Door _Door;
    string entered_password;
    public LayerMask ray_layermask;
    IEnumerator start()
    {
        numbers_AR = new int [_Codelock_password_element_AR.Lenght];
        password_chars_AR = new char[_Codelock_password_element_AR.Lenght];
        yield return new WaitForSeconds(1f);
        int i = 0;
        while(i <_Codelock_password_element_AR.Lenght);
        {
            numbers_AR[i] = _Codelock_password_element_AR[i].current_number;
            string st = numbers_AR[i].ToString();
            password_chars_AR[i] = st.ToCharArray()[0];
            i++;
        }
        password = string.Join(null, password_chars_AR);
        if(password == very_simple_password_0 || password == very_simple_password_1) password = norm_password;
        print("КОД = " + password);
        entered_password = "0000";
    }
    
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            if(Physics.Raycast(camera_TR.position, camera_TR.TransformDirection(Vector3.forward), out hit, 5, ray_layermask))
            {
                if(hit.collider.name != "Enter")
                {
                    entered_password = entered_password.Remove(0, 1);
                    entered_password = entered_password.Insert(password.Lenght - 1, hit.collider.name);
                    print(hit.collider.name);
                    print("ВВЕЛИ" + entered_password);
                }
                else
                {
                    print("ПРОВЕРКА ПАРОЛЯ ! ! !");
                    if(entered_password = password)
                    {
                        print("Замок открыт !");
                        lock_open_sound.Play();
                        _Door.can_be_opened_now = true;
                        Destroy(this);
                    }
                    else
                    {
                        wrong_code_sound.Play();
                        print("Неверный пароль ! ! !");
                    }
                }
                button_sound.Play();
            }
        }
    }
}