// Copyright © 2019 Banzai Cloud
//
// 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 file

import (
	"crypto/sha256"
	"encoding/hex"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"

	"emperror.dev/errors"
	retry "github.com/avast/retry-go"
)

func Download(u *url.URL, f string) error {
	err := retry.Do(
		func() error {
			resp, err := http.Get(u.String())
			if err != nil {
				return err
			}
			if resp.StatusCode != http.StatusOK {
				return errors.Errorf("unhandled http status code: %d", resp.StatusCode)
			}
			defer func() { _ = resp.Body.Close() }()

			out, err := os.Create(f)
			if err != nil {
				return err
			}
			defer func() { _ = out.Close() }()

			_, err = io.Copy(out, resp.Body)
			if err != nil {
				return err
			}

			return nil
		},
	)
	if err != nil {
		return err
	}

	return nil
}

func SHA256(filepath string) (string, error) {
	b, err := ioutil.ReadFile(filepath)
	if err != nil {
		return "", err
	}
	h := sha256.New()
	if _, err = h.Write(b); err != nil {
		return "", err
	}

	return hex.EncodeToString(h.Sum(nil)), nil
}

func SHA256File(f, hash string) error {
	hs, err := SHA256(f)
	if err != nil {
		return err
	}
	if hs != hash {
		return errors.Errorf("hash mismatch. got: %q, expected: %q", hs, hash)
	}

	return nil
}
