// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package firestore

import (
	"errors"

	pb "google.golang.org/genproto/googleapis/firestore/v1beta1"

	"golang.org/x/net/context"
)

// A WriteBatch holds multiple database updates. Build a batch with the Create, Set,
// Update and Delete methods, then run it with the Commit method. Errors in Create,
// Set, Update or Delete are recorded instead of being returned immediately. The
// first such error is returned by Commit.
type WriteBatch struct {
	c      *Client
	err    error
	writes []*pb.Write
}

func (b *WriteBatch) add(ws []*pb.Write, err error) *WriteBatch {
	if b.err != nil {
		return b
	}
	if err != nil {
		b.err = err
		return b
	}
	b.writes = append(b.writes, ws...)
	return b
}

// Create adds a Create operation to the batch.
// See DocumentRef.Create for details.
func (b *WriteBatch) Create(dr *DocumentRef, data interface{}) *WriteBatch {
	return b.add(dr.newReplaceWrites(data, nil, Exists(false)))
}

// Set adds a Set operation to the batch.
// See DocumentRef.Set for details.
func (b *WriteBatch) Set(dr *DocumentRef, data interface{}, opts ...SetOption) *WriteBatch {
	return b.add(dr.newReplaceWrites(data, opts, nil))
}

// Delete adds a Delete operation to the batch.
// See DocumentRef.Delete for details.
func (b *WriteBatch) Delete(dr *DocumentRef, opts ...Precondition) *WriteBatch {
	return b.add(dr.newDeleteWrites(opts))
}

// UpdateMap adds an UpdateMap operation to the batch.
// See DocumentRef.UpdateMap for details.
func (b *WriteBatch) UpdateMap(dr *DocumentRef, data map[string]interface{}, opts ...Precondition) *WriteBatch {
	return b.add(dr.newUpdateMapWrites(data, opts))
}

// UpdateStruct adds an UpdateStruct operation to the batch.
// See DocumentRef.UpdateStruct for details.
func (b *WriteBatch) UpdateStruct(dr *DocumentRef, fieldPaths []string, data interface{}, opts ...Precondition) *WriteBatch {
	return b.add(dr.newUpdateStructWrites(fieldPaths, data, opts))
}

// UpdatePaths adds an UpdatePaths operation to the batch.
// See DocumentRef.UpdatePaths for details.
func (b *WriteBatch) UpdatePaths(dr *DocumentRef, data []FieldPathUpdate, opts ...Precondition) *WriteBatch {
	return b.add(dr.newUpdatePathWrites(data, opts))
}

// Commit applies all the writes in the batch to the database atomically. Commit
// returns an error if there are no writes in the batch, if any errors occurred in
// constructing the writes, or if the Commmit operation fails.
func (b *WriteBatch) Commit(ctx context.Context) ([]*WriteResult, error) {
	if err := checkTransaction(ctx); err != nil {
		return nil, err
	}
	if b.err != nil {
		return nil, b.err
	}
	if len(b.writes) == 0 {
		return nil, errors.New("firestore: cannot commit empty WriteBatch")
	}
	db := b.c.path()
	res, err := b.c.c.Commit(withResourceHeader(ctx, db), &pb.CommitRequest{
		Database: db,
		Writes:   b.writes,
	})
	if err != nil {
		return nil, err
	}
	var wrs []*WriteResult
	for _, pwr := range res.WriteResults {
		wr, err := writeResultFromProto(pwr)
		if err != nil {
			return nil, err
		}
		wrs = append(wrs, wr)
	}
	return wrs, nil
}