//   Copyright 2023 Esri
//   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
//
//   https://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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Esri.ArcGISRuntime.Mapping;
using Map = Esri.ArcGISRuntime.Mapping.Map;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Esri.ArcGISRuntime.Geometry;

namespace DisplayAMapFromAMobileMapPackage
{
    internal class MapViewModel : INotifyPropertyChanged
    {

        public MapViewModel()
        {

            _ = SetupMap();

        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private Map _map;
        public Map Map
        {
            get => _map;
            set
            {
                _map = value;
                OnPropertyChanged();
            }
        }


        private async Task SetupMap()
        {

            // In .NET MAUI bundled files are retrieved as a read-only stream. In order to load the mobile map package it needs to be written to the
            // AppDataDirectory from where it can then be loaded to initialize the mobile map package. 
            string appDataDirectoryPathToMobileMapPackage = await GetAppDataDirectoryFilePath("Assets/MahouRivieraTrails.mmpk", "MahouRivieraTrails.mmpk.mmpk");

            // Instantiate a new mobile map package.
            MobileMapPackage mahouRivieraTrails_MobileMapPackage = new MobileMapPackage(appDataDirectoryPathToMobileMapPackage);

            // Load the mobile map package.
            await mahouRivieraTrails_MobileMapPackage.LoadAsync();

            // Show the first map in the mobile map package.
            this.Map = mahouRivieraTrails_MobileMapPackage.Maps.FirstOrDefault();

        }

        private async Task<string> GetAppDataDirectoryFilePath(string resourceFilePath, string fileName)
        {
            // Check to see if the mobile map package is already present in the AppDataDirectory.
            if (File.Exists(Path.Combine(FileSystem.Current.AppDataDirectory, fileName)))
            {
                // If the mobile map package is present in the AppDataDirectory, return the file path.
                return Path.Combine(FileSystem.Current.AppDataDirectory, fileName);
            }

            // Open the source file.
            using Stream inputStream = await FileSystem.OpenAppPackageFileAsync(resourceFilePath);

            // Create an output filename
            string targetFile = Path.Combine(FileSystem.Current.AppDataDirectory, fileName);

            // Copy the file to the AppDataDirectory
            using FileStream outputStream = File.Create(targetFile);
            await inputStream.CopyToAsync(outputStream);

            return targetFile;
        }

    }
}
