# coding=utf-8
# Copyright 2022 The Google Research Authors.
#
# 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.

"""This script converts Sintel data to the TFRecords format."""

import os
from absl import app
from absl import flags
import imageio
import numpy as np
import tensorflow as tf
from uflow.data_conversion_scripts import conversion_utils


FLAGS = flags.FLAGS
flags.DEFINE_string('data_dir', '', 'Dataset folder.')
flags.DEFINE_string('output_dir', '', 'Location to export to.')
flags.DEFINE_integer('shard', 0, 'Which shard this is.')
flags.DEFINE_integer('num_shards', 100, 'How many total shards there are.')


def convert_dataset():
  """Convert the data to the TFRecord format."""

  if not tf.io.gfile.exists(FLAGS.output_dir):
    tf.io.gfile.mkdir(FLAGS.output_dir)

  for data_split in ['training', 'test']:
    split_folder = os.path.join(FLAGS.output_dir, data_split)
    if not tf.io.gfile.exists(split_folder):
      tf.io.gfile.mkdir(split_folder)

    for data_type in ['clean', 'final']:
      output_folder = os.path.join(FLAGS.output_dir, data_split, data_type)
      if not tf.io.gfile.exists(output_folder):
        tf.io.gfile.mkdir(output_folder)

      output_folder = os.path.join(FLAGS.output_dir, data_split, data_type)
      input_folder = os.path.join(FLAGS.data_dir, data_split, data_type)
      flow_folder = os.path.join(FLAGS.data_dir, data_split, 'flow')
      occlusion_folder = os.path.join(FLAGS.data_dir, data_split, 'occlusions')
      invalid_folder = os.path.join(FLAGS.data_dir, data_split, 'invalid')

      # Directory with images.
      image_folders = sorted(tf.io.gfile.glob(input_folder + '/*'))

      if data_split == 'training':
        occlusion_folders = sorted(tf.io.gfile.glob(occlusion_folder + '/*'))
        invalid_folders = sorted(tf.io.gfile.glob(invalid_folder + '/*'))
        flow_folders = sorted(tf.io.gfile.glob(flow_folder + '/*'))
        assert len(image_folders) == len(flow_folders)
        assert len(flow_folders) == len(invalid_folders)
        assert len(invalid_folders) == len(occlusion_folders)
      else:  # Test has no ground truth flow.
        flow_folders = occlusion_folders = invalid_folders = [
            None for _ in image_folders
        ]

      data_list = []
      for image_folder, flow_folder, occlusion_folder, invalid_folder in zip(
          image_folders, flow_folders, occlusion_folders, invalid_folders):
        images = tf.io.gfile.glob(image_folder + '/*png')
        # We may want to eventually look at sequences of frames longer than 2.
        # pylint:disable=g-long-lambda
        sort_by_frame_index = lambda x: int(
            os.path.basename(x).split('_')[1].split('.')[0])
        images = sorted(images, key=sort_by_frame_index)

        if data_split == 'training':
          flows = tf.io.gfile.glob(flow_folder + '/*flo')
          flows = sorted(flows, key=sort_by_frame_index)
          occlusions = tf.io.gfile.glob(occlusion_folder + '/*png')
          occlusions = sorted(occlusions, key=sort_by_frame_index)
          invalids = tf.io.gfile.glob(invalid_folder + '/*png')
          invalids = sorted(invalids, key=sort_by_frame_index)
        else:  # Test has no ground truth flow.
          flows = occlusions = [None for _ in range(len(images) - 1)]
          invalids = [None for _ in images]

        image_pairs = zip(images[:-1], images[1:])
        invalid_pairs = zip(invalids[:-1], invalids[1:])
        # there should be 1 fewer flow images than video frames
        assert len(flows) == len(images) - 1 == len(occlusions)
        assert len(invalids) == len(images)
        data_list.extend(zip(image_pairs, flows, occlusions, invalid_pairs))

      write_records(data_list, output_folder)