/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.car.cluster;

import android.car.cluster.navigation.NavigationState.NavigationStateProto;
import android.car.cluster.renderer.InstrumentClusterRenderingService;
import android.car.cluster.renderer.NavigationRenderer;
import android.car.navigation.CarNavigationInstrumentCluster;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import android.util.Log;

import com.google.android.collect.Lists;
import com.google.protobuf.InvalidProtocolBufferException;

/**
 * Mock implementation of {@link ClusterRenderingService} to log all interaction.
 */
public class LoggingClusterRenderingService extends InstrumentClusterRenderingService {
    private static final String TAG = LoggingClusterRenderingService.class.getSimpleName();
    private static final String NAV_STATE_PROTO_BUNDLE_KEY = "navstate2";

    @Override
    public NavigationRenderer getNavigationRenderer() {
        NavigationRenderer navigationRenderer = new NavigationRenderer() {
            @Override
            public CarNavigationInstrumentCluster getNavigationProperties() {
                Log.i(TAG, "getNavigationProperties");
                CarNavigationInstrumentCluster config =
                        CarNavigationInstrumentCluster.createCluster(1000);
                config.getExtra().putIntegerArrayList("foo", Lists.newArrayList(1, 2, 3, 4));
                Log.i(TAG, "getNavigationProperties, returns: " + config);
                return config;
            }

            @Override
            public void onNavigationStateChanged(Bundle bundle) {
                StringBuilder bundleSummary = new StringBuilder();

                // Attempt to read proto byte array
                byte[] protoBytes = bundle.getByteArray(NAV_STATE_PROTO_BUNDLE_KEY);
                if (protoBytes != null) {
                    try {
                        NavigationStateProto navState = NavigationStateProto.parseFrom(protoBytes);
                        bundleSummary.append(navState.toString());

                        // Sending broadcast for testing.
                        Intent intent = new Intent(
                                "android.car.cluster.NAVIGATION_STATE_UPDATE");
                        intent.putExtra(NAV_STATE_PROTO_BUNDLE_KEY, bundle);
                        sendBroadcastAsUser(intent, UserHandle.ALL);
                    } catch (InvalidProtocolBufferException e) {
                        Log.e(TAG, "Error parsing navigation state proto", e);
                    }
                } else {
                    Log.e(TAG, "Received nav state byte array is null");
                }

                Log.i(TAG, "onEvent(" + bundleSummary + ")");
            }
        };

        Log.i(TAG, "createNavigationRenderer, returns: " + navigationRenderer);
        return navigationRenderer;
    }
}
