/*
* Naamari-lautta - a wrapper of console cloud clients for Naamari
* Copyright (c) 2015-2017 Sunkware
* Key FP = 6B6D C8E9 3438 6E9C 3D97  56E5 2CE9 A476 99EF 28F6
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* [ WWW: sunkware.org ]                         [ E-MAIL: sunkware@gmail.com ]
 */

package main

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

func Lautta(cloudClientSyntax *CloudClientSyntax, mountDirPath string, workDirPath string, errorHandleType ErrorHandleType) error {

	var err error

	var fileInfoList []os.FileInfo

	fileInfoList, err = ioutil.ReadDir(workDirPath)

	if err != nil {
		return errors.New("cannot get list of files")
	}

	// Counting actions...

	var pushNum int = 0
	var pullNum int = 0
	var uncloudNum int = 0
	var annihilateNum int = 0

	for _, fileInfo := range fileInfoList {
		if strings.HasPrefix(fileInfo.Name(), ".") {

			// make cryptomen string
			var cryptomenStr string = fileInfo.Name()[1:]

			// load cryptomen data
			var cryptomenData CryptomenData

			cryptomenData, err = ReadCryptomenData(workDirPath + "/." + cryptomenStr)
			if err != nil {
				fmt.Printf("Error: %s\n", err)
				return errors.New("cannot get cryptomen data from \"." + cryptomenStr + "\"")
			}

			switch cryptomenData.Action {

			case ACTION_NONE:

			case ACTION_PUSH:
				pushNum++

			case ACTION_PULL:
				pullNum++

			case ACTION_UNCLOUD:
				uncloudNum++

			case ACTION_ANNIHILATE:
				annihilateNum++

			default:
				fmt.Println("\"" + cryptomenStr + "\": unknown action!")
			}

		}

	}

	fmt.Printf("Push %d. Pull %d. Uncloud %d. Annihilate %d.\n", pushNum, pullNum, uncloudNum, annihilateNum)

	// Now performing the actions...

	var pushed int = 0
	var pulled int = 0
	var unclouded int = 0
	var annihilated int = 0

	for _, fileInfo := range fileInfoList {

		if strings.HasPrefix(fileInfo.Name(), ".") {

			// make cryptomen string

			var cryptomenStr string = fileInfo.Name()[1:]

			// load cryptomen data
			var cryptomenData CryptomenData

			cryptomenData, err = ReadCryptomenData(workDirPath + "/." + cryptomenStr)

			if err != nil {
				fmt.Printf("Error: %s\n", err)
				return errors.New("cannot get cryptomen data from \"." + cryptomenStr + "\"")
			}

			var actionErr error = nil

			var isPassed bool = false

			var actionRealDone bool = false

			for isPassed == false {

				switch cryptomenData.Action {

				case ACTION_NONE:

				case ACTION_PUSH:
					actionErr = Push(cloudClientSyntax, mountDirPath, workDirPath, cryptomenStr, &cryptomenData)
					if actionErr == nil {
						actionRealDone = true
						pushed++
					}

				case ACTION_PULL:
					actionErr = Pull(cloudClientSyntax, mountDirPath, workDirPath, cryptomenStr, &cryptomenData)
					if actionErr == nil {
						actionRealDone = true
						pulled++
					}

				case ACTION_UNCLOUD:
					actionErr = Uncloud(cloudClientSyntax, mountDirPath, workDirPath, cryptomenStr, &cryptomenData)
					if actionErr == nil {
						actionRealDone = true
						unclouded++
					}

				case ACTION_ANNIHILATE:
					actionErr = Annihilate(cloudClientSyntax, mountDirPath, workDirPath, cryptomenStr, &cryptomenData)
					if actionErr == nil {
						actionRealDone = true
						annihilated++
					}

				default:
					fmt.Println("\"" + cryptomenStr + "\": unknown action!")
				}

				if actionErr != nil {
					fmt.Printf("Error: %s\n", actionErr)
				}

				switch errorHandleType {
				case ON_ERROR_RETRY:
					isPassed = (actionErr == nil)

				case ON_ERROR_SKIP:
					isPassed = true

				case ON_ERROR_HALT:
					isPassed = (actionErr == nil)
					if isPassed == false {
						return errors.New("halted on failed action for \"" + cryptomenStr + "\"")
					}

				}

			}

			if actionRealDone {
				fmt.Printf("Pushed %d / %d. Pulled %d / %d. Unclouded %d / %d. Annihilated %d / %d.\n", pushed, pushNum, pulled, pullNum, unclouded, uncloudNum, annihilated, annihilateNum)
			}

		}

	}

	return nil
}
