import { types } from "./actionTypes";
import * as lunaSend from '../lunaSend';
import appinfo from "../../webos-meta/appinfo.json";

export const changeAppStatus = (status) => ({
  type: types.CHANGE_APP_STATUS,
  payload: status,
});

export const changeLocalSettings = (status) => ({
  type: types.CHANGE_LOCAL_SETTINGS,
  payload: status
});

export const getHttpHeaderForServiceRequest = onComplete => (dispatch, getState) => {
	console.log('getHttpHeaderForServiceRequest ');
	lunaSend.getHttpHeaderForServiceRequest({
		onSuccess: (res) => {
			console.log("getHttpHeaderForServiceRequest", res);
      const convertedRes = {
        "Authorization": res["Authorization"],
        "X-Authentication": res["X-Authentication"],
        "X-Device-ID": res["X-Device-ID"],
        "X-Device-Product": res["X-Device-Product"],
        "X-Device-Platform": res["X-Device-Platform"],
        "X-Device-Model": res["X-Device-Model"],
        "X-Device-Eco-Info": res["X-Device-Eco-Info"],
        "X-Device-Country": res["X-Device-Country"],
        "X-Device-Language":res["X-Device-Language"],
        "X-Device-Netcast-Platform-Version": res["X-Device-Netcast-Platform-Version"],
        "X-Device-Publish-Flag": res["X-Device-Publish-Flag"],
        "X-Device-Fck": res["X-Device-Fck"],
        "X-Device-Eula": res["X-Device-Eula"]
      }
      convertedRes["X-Device-Personalization"] = "Y";

      convertedRes["app_id"] = appinfo.id;
      //todo temp
      convertedRes["app_ver"] = "1.0.0";//appinfo.version;
      convertedRes["dvc_id"] = res["X-Device-Model"];
      convertedRes["cntry_cd"] = res["X-Device-Country"];
      convertedRes["prod_cd"] = res["X-Device-Product"];
      convertedRes["plat_cd"] =  res["X-Device-Platform"];
      convertedRes["lang_cd"] = res["X-Device-Language"];
      convertedRes["sdk_ver"] = "1.0.0"; //server sdk version
      convertedRes["publish_flag"] = "Y";
      const version = res["X-Device-Netcast-Platform-Version"] || "";
      convertedRes["os_ver"] = version;
			dispatch({ type: types.GET_HTTP_HEADER, payload: convertedRes });
      dispatch(changeAppStatus({webOSVersion: version.substring(0,version.lastIndexOf('.')), serverHOST: res["HOST"]}));
		},
		onFailure: (err) => {
			console.log("getHttpHeaderForServiceRequest fail",err);
		}
	});
};