// This example demonstrates how to authenticate with Spotify using the authorization code flow.
// In order to run this example yourself, you'll need to:
//
//  1. Register an application at: https://developer.spotify.com/my-applications/
//       - Use "http://localhost:8080/callback" as the redirect URI
//  2. Set the SPOTIFY_ID environment variable to the client ID you got in step 1.
//  3. Set the SPOTIFY_SECRET environment variable to the client secret from step 1.
package main

import (
    "fmt"
    "log"

    "github.com/dixonwille/wmenu"

    "gopkg.in/dixonwille/wlog.v2"
    "github.com/zmb3/spotify"
    "github.com/ahmetalpbalkan/go-cursor"
)

// and enter this value.
const redirectURI = "http://localhost:8080/callback"
const root_track_folder = "music/tracks/"
const root_playlists_folder = "music/playlists/"
const root_user_folder = "music/users/"

var (
    auth  = get_spotify_auth();
    ch    = make(chan *spotify.Client)
    state = "abc123"
    status = 1
    client *spotify.Client
)

func main() {

    menu := wmenu.NewMenu("Choose your Option (0-9). <return> to get back to menu.")
    menu.Action(
        func (opts []wmenu.Opt) error {
            switch opts[0].Value {
            case 1:
                HowToSetEnvVariables()
            case 2:
                fmt.Println("~ starting 'Authorization Code Flow' Sequence ~")
                client = StartAuthorizationCodeFlow()
            case 3:
                fmt.Println("~ starting 'Dowload a Track' Sequence ~")
                DownloadTrack()
            case 4:
                fmt.Println("~ starting 'Download a Playlist' Sequence ~")
                DownloadPlaylist()
            case 5:
                fmt.Println("~ starting 'Download all of my Playlists' Sequence ~")
                DownloadAllPlaylistsOfUser()
            case 6:
                fmt.Println("~ starting 'Go to webapp' Sequence~")
                startWebServer()
            }
            main()
            return nil
    })

    fmt.Print(cursor.ClearEntireScreen())
    fmt.Print(cursor.MoveTo(0,0))
    if status > 1 {
        fmt.Println("~ status => authenticated ~")
        menu.Option(" Download a Track", 3, true, nil)
        menu.Option(" Download a Playlist", 4, true, nil)
        menu.Option(" Download all of my Playlists", 5, true, nil)
        menu.Option(" Go to webapp", 6, true, nil)
    } else {
        fmt.Println("~ status => not authenticated ~")
        menu.Option(" Set Client ID", 1, true, nil)
        menu.Option(" Start Authorization Code Flow", 2, false, nil)
    }

    optionColor := wlog.None
    questionColor := wlog.Color{}
    responseColor := wlog.Color{}
    errorColor := wlog.Color{}
    menu.AddColor(optionColor, questionColor, responseColor, errorColor)
    // menu.AddColor("green", "blue", "yellow", "yellow")

    mainMenu := menu.Run()
    if mainMenu != nil{
      log.Fatal(mainMenu)
    }

}
