@RestResource(urlMapping='/UpdateAssetImage/*')
global with sharing class UpdateAssetImageRest {
	@HttpPost
    global static void doPost(String imageAsset, String imageSerial, String productSerialNo, String repairId, String repairDate, String remarkText) {
    	system.debug('UpdateAssetImageRest.start');
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        
        String imageAssetId, imageSerialId;
        String jsonResponse;
        
        if (String.isNotBlank(repairId) && String.isNotBlank(repairDate)) {
        	Repair__c repair = [Select Id, Repair_Returned_To_HP_Date__c From Repair__c Where Id= :repairId];
        	
        	if (repair == null) {
	        	res.statusCode = 200;
		        jsonResponse = '{"status": "Failure", "message": "no repair found"}';
		        res.responseBody = blob.valueOf(jsonResponse);
		        return;
        	} else {
        		repair.Repair_Returned_To_HP_Date__c = Date.parse(repairDate);
        		try {
			        update repair;
			    } catch ( Exception ex ) {
			        //TODO:
			        //error message:cannot update exception
			        res.statusCode = 200;
			        jsonResponse = '{"status": "Failure", "message": "error when try to update repair data. '+ ex +'"}';
			        res.responseBody = blob.valueOf(jsonResponse);
			        return;
			    }
        	}
        }
        
        if (String.isNotBlank(imageAsset)) {
        	List<Document > docList = [Select Id, Body, name, folderid, Type From Document Where name =:(productSerialNo + '_Asset') And folderid =:System.Label.AssetImageFolder];
        	Document dc;
        	if (docList.size() > 0) {
        		dc = docList[0];
        	} else {
        		dc = new Document();
        	}
            dc.Body = EncodingUtil.base64Decode(imageAsset);
            dc.name = productSerialNo + '_Asset';
            dc.folderid = System.Label.AssetImageFolder;
            dc.Type = 'jpg';
            try {
            	if (docList.size() > 0) { update dc; }
		        else { insert dc; }
		        imageAssetId = dc.Id;
		    } catch ( Exception ex ) {
		        //TODO:
		        //error message:cannot update exception
		        res.statusCode = 200;
		        jsonResponse = '{"status": "Failure", "message": "error when try to upsert Asset Doc. '+ ex +'"}';
		        res.responseBody = blob.valueOf(jsonResponse);
		        return;
		    }
        } else {
            //no imageAsset
        }
        
        if (String.isNotBlank(imageSerial)) {
        	List<Document > docList = [Select Id, Body, name, folderid, Type From Document Where name =:(productSerialNo + '_Serial') And folderid =:System.Label.AssetImageFolder];
        	Document dc;
        	if (docList.size() > 0) {
        		dc = docList[0];
        	} else {
        		dc = new Document();
        	}
       
            dc.Body = EncodingUtil.base64Decode(imageSerial);
            dc.name = productSerialNo + '_Serial';
            dc.folderid = System.Label.AssetImageFolder;
            dc.Type = 'jpg';
            try {
		        if (docList.size() > 0) { update dc; }
		        else { insert dc; }
		        imageSerialId = dc.Id;
		    } catch ( Exception ex ) {
		        //TODO:
		        //error message:cannot update exception
		        res.statusCode = 200;
		        jsonResponse = '{"status": "Failure", "message": "error when try to upsert Serial Doc. '+ ex +'"}';
		        res.responseBody = blob.valueOf(jsonResponse);
		        return;
		    }
        } else {
            //no imageSerial
        }
        
        Asset asset = [Select Id, ImageAsset__c, ImageSerial__c From Asset Where Product_Serial_No__c = :productSerialNo];
        if (asset == null) {
        	res.statusCode = 200;
	        jsonResponse = '{"status": "Failure", "message": "no asset found"}';
	        res.responseBody = blob.valueOf(jsonResponse);
	        return;
        } else {
        	if (String.isNotBlank(imageAssetId)) {
	        	asset.ImageAsset__c = '<img style="width:320px" src="/servlet/servlet.FileDownload?file=' + imageAssetId + '"/>';
	        	asset.ImageAssetUploadedBy__c = UserInfo.getUserId();
	        	asset.ImageAssetUploadedTime__c = Datetime.now();
	        }
	        if (String.isNotBlank(imageSerialId)) {
	        	asset.ImageSerial__c = '<img style="width:320px" src="/servlet/servlet.FileDownload?file=' + imageSerialId + '"/>';
	        	asset.ImageSerialUploadedBy__c = UserInfo.getUserId();
	        	asset.ImageSerialUploadedTime__c = Datetime.now();
	        }
	        if (String.isNotBlank(remarkText)) {
	        	asset.Remark__c = remarkText;
	        	asset.RemarkFlg__c = true;
	        }
			try {
		        update asset;
		        res.statusCode = 200;
		        jsonResponse = '{"status": "Success", "message": {"assetId": "'+ asset.Id +'", "imgaId": "'+ imageAssetId +'", "imgsId": "'+ imageSerialId +'"}}';
		        res.responseBody = blob.valueOf(jsonResponse);
		        return;
		    } catch ( Exception ex ) {
		        //TODO:
		        //error message:cannot update exception
		        res.statusCode = 200;
		        jsonResponse = '{"status": "Failure", "message": "error when try to update asset data. '+ ex +'"}';
		        res.responseBody = blob.valueOf(jsonResponse);
		        return;
		    }
        }
    }
}