/*
 * Copyright (c) 2017 Kiall Mac Innes <kiall@macinnes.ie>
 *
 * 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 ie.macinnes.tvheadend.tvinput;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.tv.TvInputService;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;

import java.util.concurrent.atomic.AtomicInteger;

import ie.macinnes.htsp.HtspMessage;
import ie.macinnes.htsp.HtspNotConnectedException;
import ie.macinnes.htsp.SimpleHtspConnection;
import ie.macinnes.tvheadend.Constants;
import ie.macinnes.tvheadend.TvContractUtils;


@RequiresApi(api = Build.VERSION_CODES.N)
public class HtspRecordingSession extends TvInputService.RecordingSession {
    private static final String TAG = HtspRecordingSession.class.getName();
    private static final int INVALID_DVR_ENTRY_ID = -1;
    private static final AtomicInteger sSessionCounter = new AtomicInteger();

    private final Context mContext;
    private final SimpleHtspConnection mConnection;
    private final int mSessionNumber;
    private final Handler mHandler;
    private final SharedPreferences mSharedPreferences;

    private Uri mChannelUri;
    private Uri mProgramUri;
    private int mDvrEntryId = INVALID_DVR_ENTRY_ID;

    public HtspRecordingSession(Context context, SimpleHtspConnection connection) {
        super(context);

        mContext = context;
        mConnection = connection;
        mSessionNumber = sSessionCounter.getAndIncrement();
        mHandler = new Handler();

        mSharedPreferences = mContext.getSharedPreferences(
                Constants.PREFERENCE_TVHEADEND, Context.MODE_PRIVATE);

        Log.d(TAG, "HtspRecordingSession created (" + mSessionNumber + ")");
    }

    @Override
    public void onTune(Uri channelUri) {
        Log.d(TAG, "RecordingSession onTune (" + mSessionNumber + ")");

        mChannelUri = channelUri;

        // I'm not sure we really need to do anything here?
        notifyTuned(channelUri);
    }

    @Override
    public void onStartRecording(@Nullable Uri programUri) {
        Log.d(TAG, "RecordingSession onStartRecording (" + mSessionNumber + ")");

        mProgramUri = programUri;

        if (mProgramUri == null || mChannelUri == null) {
            Log.e(TAG, "Failed to start recording, programUri or channelUri is null");
            return;
        }

        Integer eventId = TvContractUtils.getTvhEventIdFromProgramUri(mContext, mProgramUri);
        Integer channelId = TvContractUtils.getTvhChannelIdFromChannelUri(mContext, mChannelUri);

        if (eventId == null || channelId == null) {
            Log.e(TAG, "Failed to start recording, eventId or channelId is null");
            return;
        }

        HtspMessage addDvrEntry = new HtspMessage();
        addDvrEntry.put("method", "addDvrEntry");
        addDvrEntry.put("eventId", eventId);
        addDvrEntry.put("channelId", channelId);

        HtspMessage addDvrEntryResponse;

        try {
            addDvrEntryResponse = mConnection.sendMessage(addDvrEntry, 5000);
        } catch (HtspNotConnectedException e) {
            Log.e(TAG, "Failed to start recording, HTSP not connected", e);
            return;
        }

        mDvrEntryId = addDvrEntryResponse.getInteger("id");
        Log.i(TAG, "DVR Entry created with ID: " + mDvrEntryId);
    }

    @Override
    public void onStopRecording() {
        Log.d(TAG, "RecordingSession onStopRecording (" + mSessionNumber + ")");

        if (mDvrEntryId == INVALID_DVR_ENTRY_ID) {
            Log.e(TAG, "Failed to stop recording, no known DvrEntryId");
            return;
        }

        HtspMessage cancelDvrEntry = new HtspMessage();
        cancelDvrEntry.put("method", "cancelDvrEntry");
        cancelDvrEntry.put("id", mDvrEntryId);

        try {
            mConnection.sendMessage(cancelDvrEntry);
        } catch (HtspNotConnectedException e) {
            Log.e(TAG, "Failed to stop recording, HTSP not connected", e);
        }

        notifyRecordingStopped(null);
    }

    @Override
    public void onRelease() {
        Log.d(TAG, "RecordingSession onRelease (" + mSessionNumber + ")");
    }
}

@@ -731,16 +731,56 @@
 rences;%0A
+import android.media.tv.TvInputManager;%0A
 import a
@@ -3877,35 +3877,197 @@
         
-return;%0A        %7D%0A%0A
+notifyError(TvInputManager.RECORDING_ERROR_UNKNOWN);%0A            return;%0A        %7D%0A%0A        boolean success = addDvrEntryResponse.getBoolean(%22success%22);%0A%0A        if (success) %7B%0A    
         
@@ -4114,24 +4114,28 @@
 eger(%22id%22);%0A
+    
         Log.
@@ -4183,24 +4183,264 @@
 vrEntryId);%0A
+        %7D else %7B%0A            String error = addDvrEntryResponse.getString(%22error%22, %22Unknown error%22);%0A            Log.e(TAG, %22Failed to create DVR Entry: %22 + error);%0A            notifyError(TvInputManager.RECORDING_ERROR_UNKNOWN);%0A        %7D%0A
     %7D%0A%0A    @

Handle failure to create DvrEntry