using Nakov.TurtleGraphics;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Turtle_ScreenSaver
{
    public partial class Form1 : Form
    {
        private Timer timer;
        private Random rnd;
        private int exitCount;

        public Form1()
        {
            InitializeComponent();

            // Timer 초기화
            timer = new Timer();
            timer.Interval = 200; // 0.2초마다 이벤트 발생
            timer.Tick += Timer_Tick;

            // Random 객체 초기화
            rnd = new Random();

            exitCount = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None; // 창 테두리 제거
            this.WindowState = FormWindowState.Maximized; // 창 사이즈 최대

            // Timer 시작
            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
						// 폼의 넓이와 높이, 색상, 각도, 거리, 좌표
            int swidth = this.ClientSize.Width, sheight = this.ClientSize.Height;
            int r, g, b, angle, dist;
            float curX, curY;
						
						// RGB 값
            r = rnd.Next(0, 256);
            g = rnd.Next(0, 256);
            b = rnd.Next(0, 256);
            Turtle.PenColor = Color.FromArgb(r, g, b);

						// 각, 거리 값
            angle = rnd.Next(0, 360);
            dist = rnd.Next(50, 200);
            Turtle.Rotate(angle);
            Turtle.Forward(dist);
            curX = Turtle.X;
            curY = Turtle.Y;
						
						// 화면 경계 벗어났는지 확인
            if ((-swidth / 2 <= curX && curX <= swidth / 2) && (-sheight / 2 <= curY && curY <= sheight / 2))
            {

            }
						// 경계 밖이라면 원점으로 이동시킨 뒤 다시 그리게
            else
            {
                Turtle.PenUp();
                Turtle.MoveTo(0, 0);
                Turtle.PenDown();

                exitCount++;
                if (exitCount == 50)
                {
                    timer.Stop(); 
                }
            }
        }
				
				// 마우스 클릭 시 화면보호기 끄기
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                Application.Exit();
            }
        }
    }
}