package api

import (
	"../settings"
	"encoding/json"
	"fmt"
    "net/http"
    "../schedule"
	// "strconv"
	// "errors"
	// "math/rand"
)

var tmpteachers []schedule.Teacher
var tmpcabinets []schedule.Cabinet

var tmpgroups []schedule.Group
var tmpsubjects []schedule.Subject
// var tmplessons []schedule.LessonJSON

func GetDataTeachers(w http.ResponseWriter, r *http.Request) {

	var classrooms []Classroom
	var teachers []Teacher
	settings.DB.Find(&teachers)
	settings.DB.Find(&classrooms)
	var cabinet schedule.Cabinet
	for _, room := range classrooms{
		cabinet.Name = room.Name
		cabinet.IsComputer = room.Iscomputer
		tmpcabinets = append(tmpcabinets, cabinet)
	}

	for _, teacher := range teachers{
		var scheduleTeacher schedule.Teacher
		scheduleTeacher.Name = teacher.Name + " " + string(teacher.Surname[:2]) + "." + string(teacher.Patronymic[:2]) + "."
		for _, room := range classrooms{
			if teacher.IDClassroom == room.ID {
				var tmpcabinet schedule.Cabinet
				tmpcabinet.Name = room.Name
				tmpcabinet.IsComputer = room.Iscomputer
				scheduleTeacher.Cabinets = append(scheduleTeacher.Cabinets, tmpcabinet)
				tmpcabinet = schedule.Cabinet{}
				break 
			}
		}
		tmpteachers = append(tmpteachers, scheduleTeacher) 
	}

	json.NewEncoder(w).Encode(tmpteachers)
	return
}

func GetDataGroups(w http.ResponseWriter, r *http.Request){
	// tmp := replacePath(r.URL.Path, "/api/getgroups/")
	// num, err := strconv.Atoi(tmp)

	// if err != nil{
	// 	json.NewEncoder(w).Encode(struct{ Error string }{Error: "strconv error"})
	// }
	var groups []Group
	var subjects []Subject
	// var tmpsubofgroup1 []Subjectofgroup
	// var tmpsubofgroup2 []Subjectofgroup
	var tmpsubofgroup []Subjectofgroup

	settings.DB.Find(&groups)
	settings.DB.Find(&subjects)
	// settings.DB.Where("id_group = ?", num).Find(&tmpsubofgroup)

	for _, group := range groups{
		settings.DB.Where("id_group = ?", group.ID).Find(&tmpsubofgroup)

		var tmpgroup schedule.Group
		tmpgroup.Name = group.Groupnumber
		tmpgroup.Quantity = group.Studentsquantity
		for _, subject := range tmpsubofgroup{
			// scheduleTeacher.Name = teacher.Name + " " + string(teacher.Surname[:2]) + "." + string(teacher.Patronymic[:2]) + "."
			var tmpsubject schedule.Subject
			var tsubject Subject
			var teacher Teacher
			var semester Semester
			settings.DB.Where("id = ?", subject.ID).First(&tsubject)
			settings.DB.Where("id = ?", subject.IDTeacher).First(&teacher)
			settings.DB.Where("id = ?", subject.IDSemester).First(&semester)
			tmpsubject.Name = tsubject.Shortname
			tmpsubject.Teacher = teacher.Name + " " + string(teacher.Surname[:2]) + "." + string(teacher.Patronymic[:2]) + "."
			tmpsubject.Teacher2 = tmpsubject.Teacher // TODO
			if tsubject.IDStype == 1{
				tmpsubject.IsComputer = false
				tmpsubject.Theory = 0
				tmpsubject.Practice.A = subject.Hoursquantity/2
				tmpsubject.Practice.B = subject.Hoursquantity/2
			} else if tsubject.IDStype == 0{
				tmpsubject.IsComputer = true
				tmpsubject.Theory = subject.Hoursquantity/2
				tmpsubject.Practice.A = 0
				tmpsubject.Practice.B = 0
			}
			fmt.Println(semester)
			tmpsubject.WeekLessons.A = (subject.Hoursquantity/semester.Weeksquantity)/2
			tmpsubject.WeekLessons.B = (subject.Hoursquantity/semester.Weeksquantity)/2
			tmpsubjects = append(tmpsubjects, tmpsubject)
		}
	}

	// for i, sub := range tmpsubofgroup1{
	// 	var subof Subjectofgroup
	// 	for j, gr := range tmpsubofgroup2{
	// 		if gr == sub{
	// 			subof = gr
	// 			tmpsubofgroup1 = append(tmpsubofgroup1[:i], tmpsubofgroup[i+1:]...)
	// 			tmpsubofgroup2 = append(tmpsubofgroup1[:j], tmpsubofgroup[j+1:]...)
	// 			break
	// 		}
	// 	}
	// 	tmpsubofgroup = append(tmpsubofgroup, subof)
	// 	subof = Subjectofgroup{}
	// }

	json.NewEncoder(w).Encode(tmpsubjects)
	// for _, subject := range subjects{
	// 	var tmpsubject schedue.Subject
	// 	tmpsubject.Name = subject.ShortName
	// 	tmpsubject.teacher = 
	// }
}
