﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApp3
{
    

    public partial class Form1 : Form
    {
        // private XmlTextWriter xtw;
        private XmlWriter xtw;
        public Form1()
        {
            InitializeComponent();

            cmbRating.SelectedIndex = 3;

            //xtw = new XmlTextWriter(@"C:\ucenik\Filmovi.xml");

            xtw = XmlWriter.Create(@"C:\ucenik\Filmovi.xml");

            // pocetak XML dokumenta>
            xtw.WriteStartDocument();

            xtw.WriteComment("Biblioteka filmova");
            xtw.WriteComment("Ovo je fajl i kojem je nabrojano dosta video snimaka");

            // root tag / koreni element
            xtw.WriteStartElement("VideoBiblioteka");


        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            // zatvara root tag
            xtw.WriteEndElement();

            xtw.WriteEndDocument();// zatvara ceo dokument

            xtw.Flush(); // da sve iz toka podataka ode gde treba
            xtw.Close(); // zatvaranje writera

            Application.Exit(); // izlazak iz aplikacije
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            xtw.WriteStartElement("Video"); // otv.tag Video elementa
            // element Naslov
            xtw.WriteElementString("Naslov", txtTitle.Text);
            
            // element Trajanje:
            xtw.WriteStartElement("Trajanje");
            xtw.WriteAttributeString("Mera", "minute");
            xtw.WriteString(txtLength.Text);
            xtw.WriteEndElement();

            // element Zvezda
            xtw.WriteElementString("Zvezda", txtStar.Text);

            // element Rejting
            xtw.WriteElementString("rejting", cmbRating.Text);

            xtw.WriteEndElement(); // zatv.tag za Video
            
            txtTitle.Focus(); // kursor u prvi element

            txtTitle.Text = "";
            txtLength.Text = "";
            txtStar.Text = "";

        }
    }
}
