/**
 * Copyright (C) 2014 Deepin Technology Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 **/

package main

import (
	"fmt"
	"gir/gio-2.0"
	"os"
	"path"
	"pkg.deepin.io/lib/mime"
	dutils "pkg.deepin.io/lib/utils"
)

type AppInfo struct {
	// Desktop id
	Id string
	// App name
	Name string
	// Display name
	DisplayName string
	// Comment
	Description string
	// Icon
	Icon string
	// Commandline
	Exec string
}
type AppInfos []*AppInfo

func GetAppInfo(ty string) (*AppInfo, error) {
	id, err := mime.GetDefaultApp(ty, false)
	if err != nil {
		return nil, err
	}

	info, err := newAppInfoById(id)
	if err != nil {
		return nil, err
	}
	return info, nil
}

func (infos AppInfos) Delete(id string) AppInfos {
	var ret AppInfos
	for _, info := range infos {
		if info.Id == id {
			continue
		}
		ret = append(ret, info)
	}
	return ret
}

func SetAppInfo(ty, id string) error {
	return mime.SetDefaultApp(ty, id)
}

func GetAppInfos(ty string) AppInfos {
	var infos AppInfos
	for _, id := range mime.GetAppList(ty) {
		appInfo, err := newAppInfoById(id)
		if err != nil {
			logger.Warning(err)
			continue
		}
		infos = append(infos, appInfo)
	}
	return infos
}

func newAppInfoById(id string) (*AppInfo, error) {
	ginfo := gio.NewDesktopAppInfo(id)
	if ginfo == nil {
		id = "kde4-" + id
		ginfo = gio.NewDesktopAppInfo(id)
	}
	if ginfo == nil {
		return nil, fmt.Errorf("gio.NewDesktopAppInfo failed: id %v", id)
	}

	defer ginfo.Unref()
	var info = &AppInfo{
		Id:          id,
		Name:        ginfo.GetName(),
		DisplayName: ginfo.GetGenericName(),
		Description: ginfo.GetDescription(),
		Exec:        ginfo.GetCommandline(),
	}
	iconObj := ginfo.GetIcon()
	if iconObj != nil {
		info.Icon = iconObj.ToString()
		iconObj.Unref()
	}

	return info, nil
}

func findFilePath(file string) string {
	data := path.Join(os.Getenv("HOME"), ".local/share", file)
	if dutils.IsFileExist(data) {
		return data
	}

	data = path.Join("/usr/local/share", file)
	if dutils.IsFileExist(data) {
		return data
	}

	return path.Join("/usr/share", file)
}
