package aliyun

import (
	. "code.hoteas.com/golang/hotime/common"
	"fmt"
	"io/ioutil"
	"net/http"
	//"fmt"
)

type company struct {
	ApiCode string
	Url     string
}

var Company = company{}

func (that *company) Init(apiCode string) {
	//"06c6a07e89dd45c88de040ee1489eef7"
	that.ApiCode = apiCode
	that.Url = "http://api.81api.com"
}

// GetCompanyOtherAll  获取企业基础信息
func (that *company) GetCompanyOtherAll(name string) Map {

	res := Map{}
	data, e := that.GetCompanyPatentsInfo(name) //获取专利信息
	if e != nil {
		fmt.Println(e)
	} else {
		res["PatentsInfo"] = data.GetMap("data")
	}
	data, e = that.GetCompanyOtherCopyrightsInfo(name) //获取其他专利
	if e != nil {
		fmt.Println(e)
	} else {
		res["OtherCopyrightsInfo"] = data.GetMap("data")
	}
	data, e = that.GetCompanyTrademarksInfo(name) //获取商标
	if e != nil {
		fmt.Println(e)
	} else {
		res["TrademarksInfo"] = data.GetMap("data")
	}
	data, e = that.GetCompanySoftwareCopyrightsInfo(name) //获取软著
	if e != nil {
		fmt.Println(e)
	} else {
		res["SoftwareCopyrightsInfo"] = data.GetMap("data")
	}
	data, e = that.GetCompanyProfileTags(name) //获取大数据标签
	if e != nil {
		fmt.Println(e)
	} else {
		res["ProfileTags"] = data.GetSlice("data")
	}
	return res
}

// GetCompanyBaseInfo 获取企业基础信息
func (that *company) GetCompanyList(name string) (Map, error) {
	url := "/fuzzyQueryCompanyInfo/"

	body, err := that.basePost(url, name)
	return ObjToMap(body), err
}

// GetCompanyBaseInfo 获取企业基础信息
func (that *company) GetCompanyBaseInfo(name string) (Map, error) {
	url := "/getCompanyBaseInfo/"

	body, err := that.basePost(url, name)
	return ObjToMap(body), err
}

// GetCompanyPatentsInfo  获取专利信息
func (that *company) GetCompanyPatentsInfo(name string) (Map, error) {
	url := "/getCompanyPatentsInfo/"

	body, err := that.basePost(url, name)
	return ObjToMap(body), err
}

// GetCompanyTrademarksInfo 获取商标信息
func (that *company) GetCompanyTrademarksInfo(name string) (Map, error) {
	url := "/getCompanyTrademarksInfo/"

	body, err := that.basePost(url, name)
	return ObjToMap(body), err
}

// GetCompanySoftwareCopyrightsInfo 获取软著信息
func (that *company) GetCompanySoftwareCopyrightsInfo(name string) (Map, error) {
	url := "/getCompanySoftwareCopyrightsInfo/"

	body, err := that.basePost(url, name)
	return ObjToMap(body), err
}

// GetCompanyOtherCopyrightsInfo 获取其他著作信息
func (that *company) GetCompanyOtherCopyrightsInfo(name string) (Map, error) {
	url := "/getCompanyOtherCopyrightsInfo/"

	body, err := that.basePost(url, name)
	return ObjToMap(body), err
}

// GetCompanyProfileTags 获取大数据标签
func (that *company) GetCompanyProfileTags(name string) (Map, error) {
	url := "/getCompanyProfileTags/"
	body, err := that.basePost(url, name)
	return ObjToMap(body), err
}
func (that *company) basePost(url string, name string) (string, error) {

	client := &http.Client{}

	reqest, err := http.NewRequest("GET", that.Url+url+name+"/?isRaiseErrorCode=1", nil)

	if err != nil {
		fmt.Println("Fatal error ", err.Error())
		return "", err
	}

	reqest.Header.Add("Authorization", "APPCODE "+that.ApiCode)
	response, err := client.Do(reqest)
	defer response.Body.Close()

	if err != nil {
		fmt.Println("Fatal error ", err.Error())
		return "", err
	}

	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return "", err
	}
	res := string(body)
	fmt.Println(res)
	return res, err
}
