package com.huawei.esdk.fusionmanager.local.impl.resources.net;

//import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.huawei.esdk.fusionmanager.local.impl.SDKClient;
import com.huawei.esdk.fusionmanager.local.impl.constant.ESDKURL;
import com.huawei.esdk.fusionmanager.local.impl.constant.NativeConstant;
import com.huawei.esdk.fusionmanager.local.impl.utils.ClientProvider;
import com.huawei.esdk.fusionmanager.local.impl.utils.StringUtils;
import com.huawei.esdk.fusionmanager.local.impl.utils.URLUtils;
import com.huawei.esdk.fusionmanager.local.model.ClientProviderBean;
import com.huawei.esdk.fusionmanager.local.model.net.CreateIPBWTemplateReq;
import com.huawei.esdk.fusionmanager.local.model.net.CreateIPBWTemplateResp;
import com.huawei.esdk.fusionmanager.local.model.net.DeleteIPBWTemplateResp;
import com.huawei.esdk.fusionmanager.local.model.net.ListIPBWTemplateReqEx;
import com.huawei.esdk.fusionmanager.local.model.net.ListIPBWTemplateResp;
import com.huawei.esdk.fusionmanager.local.model.net.QueryIPBWTemplateResp;
import com.huawei.esdk.fusionmanager.local.model.net.UpdateIPBWTemplateReq;
import com.huawei.esdk.fusionmanager.local.model.net.UpdateIPBWTemplateResp;
import com.huawei.esdk.fusionmanager.local.resources.net.IPBWTemplateResource;

/**
 * IP带宽模板接口实现类。
 * 
 * @since eSDK Cloud V100R003C30
 */
public class IPBWTemplateResourceImpl implements IPBWTemplateResource
{
    //private static final Logger LOGGER = Logger.getLogger(IPBWTemplateResourceImpl.class);
    
    private static IPBWTemplateResourceImpl instance = null;
    
    private Gson gson = new GsonBuilder().serializeNulls().create();
    
    private SDKClient sdkRestClient;
    
    private ClientProviderBean bean;
    
    public static synchronized IPBWTemplateResourceImpl getInstance(ClientProviderBean bean)
    {
        if (null == instance)
        {
            instance = new IPBWTemplateResourceImpl();
        }
        
        instance.init(bean);
        return instance;
    }
    
    private IPBWTemplateResourceImpl()
    {
    }
    
    private void init(ClientProviderBean bean)
    {
        this.bean = bean;
        sdkRestClient = (SDKClient)ClientProvider.getClient(bean);
    }
    
    @Override
    public CreateIPBWTemplateResp create(CreateIPBWTemplateReq request)
    {
        //LOGGER.info("create() start");
        String json = null;
        CreateIPBWTemplateResp response = null;
        
        try
        {
            json =
                sdkRestClient.post(URLUtils.makeUrl(bean, ESDKURL.IPBWTEMPLATERESOURCE_CREATE, null),
                    request,
                    "IPBWTemplateResource#Create");
            response = gson.fromJson(json, CreateIPBWTemplateResp.class);
        }
        catch (JsonSyntaxException e)
        {
            //LOGGER.error("create() JsonSyntaxException", e);
            response = new CreateIPBWTemplateResp();
            response.setCode(NativeConstant.SERVER_EXCEPTION);
        }
        catch (Exception e)
        {
            //LOGGER.error("create() error", e);
            response = new CreateIPBWTemplateResp();
            response.setCode(NativeConstant.NATIVE_EXCEPTION);
        }
        //LOGGER.info("create() resp:" + gson.toJson(response));
        //LOGGER.info("create() end");
        return response;
    }
    
    @Override
    public DeleteIPBWTemplateResp remove(String ipbwtemplateId)
    {
        //LOGGER.info("remove() start");
        String json = null;
        DeleteIPBWTemplateResp response = null;
        
        try
        {
            json =
                sdkRestClient.delete(URLUtils.makeUrl(bean,
                    ESDKURL.IPBWTEMPLATERESOURCE_REMOVE,
                    new String[] {ipbwtemplateId}), "IPBWTemplateResource#Remove");
            response = gson.fromJson(json, DeleteIPBWTemplateResp.class);
        }
        catch (JsonSyntaxException e)
        {
            //LOGGER.error("remove() JsonSyntaxException", e);
            response = new DeleteIPBWTemplateResp();
            response.setCode(NativeConstant.SERVER_EXCEPTION);
        }
        catch (Exception e)
        {
            //LOGGER.error("remove() error", e);
            response = new DeleteIPBWTemplateResp();
            response.setCode(NativeConstant.NATIVE_EXCEPTION);
            
        }
        //LOGGER.info("remove() resp:" + gson.toJson(response));
        //LOGGER.info("remove() end");
        return response;
    }
    
    @Override
    public ListIPBWTemplateResp list(String vdcId, ListIPBWTemplateReqEx requestEx)
    {
        //LOGGER.info("query() start");
        String json = null;
        ListIPBWTemplateResp response = null;
        String url = null;
        try
        {
            url =
                URLUtils.makeUrl(bean, ESDKURL.IPBWTEMPLATERESOURCE_LIST, new String[] {vdcId,
                    StringUtils.convertString(requestEx.getStart()), StringUtils.convertString(requestEx.getLimit()),
                    requestEx.getName()});
            json = sdkRestClient.get(url, "IPBWTemplateResource#List");
            response = gson.fromJson(json, ListIPBWTemplateResp.class);
            
        }
        catch (JsonSyntaxException e)
        {
            //LOGGER.error("query() JsonSyntaxException", e);
            response = new ListIPBWTemplateResp();
            response.setCode(NativeConstant.SERVER_EXCEPTION);
        }
        catch (Exception e)
        {
            //LOGGER.error("query() error", e);
            response = new ListIPBWTemplateResp();
            response.setCode(NativeConstant.NATIVE_EXCEPTION);
        }
        //LOGGER.info("query() resp:" + gson.toJson(response));
        //LOGGER.info("query() end");
        return response;
    }
    
    @Override
    public QueryIPBWTemplateResp query(String vdcId, String ipbwtemplateId)
    {
        //LOGGER.info("query() start");
        String json = null;
        QueryIPBWTemplateResp response = null;
        String url = null;
        try
        {
            url = URLUtils.makeUrl(bean, ESDKURL.IPBWTEMPLATERESOURCE_QUERY, new String[] {vdcId, ipbwtemplateId});
            json = sdkRestClient.get(url, "IPBWTemplateResource#Query");
            response = gson.fromJson(json, QueryIPBWTemplateResp.class);
            
        }
        catch (JsonSyntaxException e)
        {
            //LOGGER.error("query() JsonSyntaxException", e);
            response = new QueryIPBWTemplateResp();
            response.setCode(NativeConstant.SERVER_EXCEPTION);
        }
        catch (Exception e)
        {
            //LOGGER.error("query() error", e);
            response = new QueryIPBWTemplateResp();
            response.setCode(NativeConstant.NATIVE_EXCEPTION);
        }
        //LOGGER.info("query() resp:" + gson.toJson(response));
        //LOGGER.info("query() end");
        return response;
    }
    
    @Override
    public UpdateIPBWTemplateResp modify(UpdateIPBWTemplateReq request, String ipbwtemplateId)
    {
        //LOGGER.info("modify() start");
        String json = null;
        UpdateIPBWTemplateResp response = null;
        
        try
        {
            json =
                sdkRestClient.put(URLUtils.makeUrl(bean,
                    ESDKURL.IPBWTEMPLATERESOURCE_MODIFY,
                    new String[] {ipbwtemplateId}), request, "IPBWTemplateResource#Modify");
            response = gson.fromJson(json, UpdateIPBWTemplateResp.class);
        }
        catch (JsonSyntaxException e)
        {
            //LOGGER.error("modify() JsonSyntaxException", e);
            response = new UpdateIPBWTemplateResp();
            response.setCode(NativeConstant.SERVER_EXCEPTION);
        }
        catch (Exception e)
        {
            //LOGGER.error("modify() error", e);
            response = new UpdateIPBWTemplateResp();
            response.setCode(NativeConstant.NATIVE_EXCEPTION);
        }
        //LOGGER.info("modify() resp:" + gson.toJson(response));
        //LOGGER.info("modify() end");
        return response;
    }
    
}
