﻿using UnityEngine;
using UnityEngine.UI;
using System.IO;

public class DeviceCamera : MonoBehaviour {
	private new WebCamTexture camera;
	private Quaternion baserotation;
	private int number_of_photos;
	private bool draw;

	// Use this for initialization
	void Start () {
		camera = new WebCamTexture();
		GetComponent<Renderer>().material.mainTexture = camera;
		camera.Play();
		number_of_photos = Directory.GetFiles("C:/Users/Public/Pictures/").Length - 1;
		draw = GameObject.Find("Pen").GetComponentInChildren<Toggle>().isOn;
	}

	public void TakePhoto()
	{
		Texture2D photo = new Texture2D(camera.width, camera.height);
		photo.SetPixels(camera.GetPixels());
		photo.Apply();
		byte[] bytes = photo.EncodeToPNG();
		File.WriteAllBytes("C:/Users/Public/Pictures/" + "Snapchat " + number_of_photos + ".png", bytes);
	}

	public void Draw()
	{
		if (draw)
		{

		}
	}
	
	// Update is called once per frame
	void Update () {

	}
}
