package database

import (
	"database/sql"

	_ "github.com/go-sql-driver/mysql"
)

//DB : Conexão com db
var DB *sql.DB

//Config : struct do banco de dados
type Config struct {
	Host   string
	Port   string
	User   string
	Pass   string
	Schema string
}

var IsConnected bool
var Error error

//Init : iniciação do metodo de conexão
func Init(config Config) error {

	//user:pass@tcp(host:port)/schema?parseTime=true
	var dataSource = config.User + ":" +
		config.Pass + "@tcp(" +
		config.Host + ":" +
		config.Port + ")/" +
		config.Schema + "?parseTime=true"

	var err error

	DB, err = sql.Open("mysql", dataSource)

	if err != nil {
		IsConnected = false
		Error = err
	}

	if err = DB.Ping(); err != nil {
		IsConnected = false
		Error = err
	}

	return err
}
