package eta_forum

import (
	"encoding/json"
	"eta/eta_mobile/models"
	"eta/eta_mobile/utils"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

// GetUserChartListLib 查询有权限的图表列表
func GetUserChartListLib(req string) (resp *UserChartListResp, err error) {
	_, resultByte, err := get(req, "/v1/chart/user/chart_list")
	err = json.Unmarshal(resultByte, &resp)
	if err != nil {
		return
	}
	return
}

// getUserCollectChartClassifyLib 查询收藏的分类列表
func getUserCollectChartClassifyLib(req string) (resp *UserCollectChartClassifyListResp, err error) {
	_, resultByte, err := get(req, "/v1/chart_collect/classify/list")
	err = json.Unmarshal(resultByte, &resp)
	if err != nil {
		return
	}
	return
}

// getUserCollectChartListLib 查询收藏的图表列表
func getUserCollectChartListLib(req string) (resp *UserCollectChartListResp, err error) {
	_, resultByte, err := get(req, "/v1/chart_collect/chart/list")
	err = json.Unmarshal(resultByte, &resp)
	if err != nil {
		return
	}
	return
}

// getChartFromUniqueCodeLib 根据唯一编码查询图表
func getChartFromUniqueCodeLib(req string) (resp ChartFromUniqueCodeResp, err error) {
	_, resultByte, err := get(req, "/v1/chart/common/from_unique_code")
	err = json.Unmarshal(resultByte, &resp)
	if err != nil {
		return
	}
	return
}

// ChartClassifySaveLib 上传图表分类信息
func ChartClassifySaveLib(req string) (resp *models.BaseResponse, err error) {
	_, resultByte, err := post(req, "/v1/chart_classify/save")
	err = json.Unmarshal(resultByte, &resp)
	if err != nil {
		return
	}
	return
}

// ChartClassifySaveBatchLib 批量上传图表分类信息
func ChartClassifySaveBatchLib(req string) (resp *models.BaseResponse, err error) {
	_, resultByte, err := post(req, "/v1/chart_classify/batch_save")
	err = json.Unmarshal(resultByte, &resp)
	if err != nil {
		return
	}
	return
}
// ChartDeleteLib 从社区撤回图表
func ChartDeleteLib(req string) (resp *models.BaseResponse, err error) {
	_, resultByte, err := post(req, "/v1/chart/delete")
	err = json.Unmarshal(resultByte, &resp)
	if err != nil {
		return
	}
	return
}
// get
func get(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
	if utils.ETA_FORUM_HUB_URL == "" {
		err = fmt.Errorf("ETA社区桥接服务地址为空")
		return
	}
	urlStr = urlStr + "?" + paramStr
	getUrl := utils.ETA_FORUM_HUB_URL + urlStr
	result, err = HttpGet(getUrl)
	if err != nil {
		err = fmt.Errorf("调用ETA社区桥接服务接口失败 error:%s", err.Error())
		return
	}
	err = json.Unmarshal(result, &resp)
	if err != nil {
		return
	}

	return
}

func HttpGet(url string) ([]byte, error) {
	client := &http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("authorization", utils.MD5(utils.ETA_FORUM_HUB_NAME_EN+utils.ETA_FORUM_HUB_MD5_KEY))
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	utils.FileLog.Debug("HttpPost:" + string(b))
	return b, err
}

func post(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
	if utils.ETA_FORUM_HUB_URL == "" {
		err = fmt.Errorf("ETA社区桥接服务地址为空")
		return
	}
	postUrl := utils.ETA_FORUM_HUB_URL + urlStr
	result, err = HttpPost(postUrl, paramStr, "application/json")
	if err != nil {
		err = fmt.Errorf("调用ETA社区桥接服务接口失败 error:%s", err.Error())
		return
	}
	err = json.Unmarshal(result, &resp)
	if err != nil {
		return
	}

	return
}

func HttpPost(url, postData string, params ...string) ([]byte, error) {
	body := ioutil.NopCloser(strings.NewReader(postData))
	client := &http.Client{}
	req, err := http.NewRequest("POST", url, body)
	if err != nil {
		return nil, err
	}
	contentType := "application/x-www-form-urlencoded;charset=utf-8"
	if len(params) > 0 && params[0] != "" {
		contentType = params[0]
	}
	req.Header.Set("Content-Type", contentType)
	req.Header.Set("authorization", utils.MD5(utils.ETA_FORUM_HUB_NAME_EN+utils.ETA_FORUM_HUB_MD5_KEY))
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	utils.FileLog.Debug("HttpPost:" + string(b))
	return b, err
}
