//@author  Ajit Jagtap
//@version Wed Jul 04 2018 20:15:13 GMT+0530 (IST)

// Package dalmdl will help you access
package dalmdl

import (
	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/dalmdl/fdb"
	"github.com/tidwall/gjson"
)

// BaseDAO help to fetch data
//TODO: Add coreerror object
type BaseDAO struct {
	filePath    string
	query       []string
	Output      gjson.Result
	isCacheable bool
}

// DAOBuilder will help to run gson query
type DAOBuilder struct {
	BaseDAO
}

// GetDAO return new instance
func GetDAO() *DAOBuilder {
	return &DAOBuilder{}
}

// Run : Execute Query
//TODO: Before Run() call validate method
func (db *DAOBuilder) Run() *gjson.Result {
	db.Output = gjson.Result{}
	for _, qry := range db.query {
		// FIXME:Return error. Error handling remaining.
		db.Output, _ = fdb.GetDataDAO(db.filePath, qry, db.isCacheable, db.Output)
	}

	return &db.Output
}

// FilePath to open File and get data from FDB
func (db *DAOBuilder) FilePath(name string) *DAOBuilder {
	db.BaseDAO.filePath = name
	return db
}

// IsCacheable to open File and get data from FDB
func (db *DAOBuilder) IsCacheable(isCacheable bool) *DAOBuilder {
	db.BaseDAO.isCacheable = isCacheable
	return db
}

// Query give multiple queries for and condition
func (db *DAOBuilder) Query(query ...string) *DAOBuilder {
	db.BaseDAO.query = query
	return db
}