using GMCabsDriverAssistant.Services;
using GMCabsDriverAssistant.Utils;

namespace GMCabsDriverAssistantSolution.Views;

public partial class ScanDrivingLicencePage : ContentPage
{
    //private FileResult drivingLicencePhoto;
    private string drivingLicencePhoto;
    private Stream _stream;
    private GMCabsDriverService _service;

    public ScanDrivingLicencePage()
    {
        InitializeComponent();
        _service = new GMCabsDriverService();
    }

    private void ScanDrivingLicence_Clicked(object sender, EventArgs e)
    {
        try
        {
            if (MediaPicker.IsCaptureSupported)
            {
                //string action = await DisplayActionSheet("Scan Driving licence?", "Cancel", null , "Capture Photo", "From Gallery");
                //if (action == "Cancel")
                //{
                //    return;
                //}
                //drivingLicencePhoto = action == "Capture Photo"? await MediaPicker.CapturePhotoAsync() : await MediaPicker.PickPhotoAsync();

                //new ImageCropper()
                //{
                //    PageTitle = "Crop Photo",
                //    AspectRatioX = 3,
                //    AspectRatioY = 2,
                //    CropShape = ImageCropper.CropShapeType.Rectangle,
                //    SelectSourceTitle = "Select source",
                //    TakePhotoTitle = "Capture Photo",
                //    PhotoLibraryTitle = "From Gallery",
                //    Success = (imageFile) =>
                //    {
                //        Device.BeginInvokeOnMainThread(() =>
                //        {
                //            resultImage.Source = ImageSource.FromFile(imageFile);
                //            if (resultImage.Source != null)
                //            {
                //                drivingLicencePhoto = imageFile;
                //                BtnUpdateProfile.IsEnabled = true;
                //            }
                //        });
                //    }
                //}.Show(this);


                //if (drivingLicencePhoto != null)
                //{
                //    _stream = await drivingLicencePhoto.OpenReadAsync();

                //    resultImage.Source = ImageSource.FromStream(() => _stream);

                //    if (resultImage.Source != null)
                //    {
                //        BtnUpdateProfile.IsEnabled = true;
                //    }
                //}
            }
        }
        catch (Exception ex) { }
    }
    private async void BtnUpdateProfile_Clicked(object sender, EventArgs e)
    {
        if (drivingLicencePhoto == null)
        {
            return;
        }

        string appToken = Preferences.Get(SecureStorageData.Token, "");

        var content = new MultipartFormDataContent();
        string[] res = drivingLicencePhoto.Split('/');
        if (res.Length > 0)
        {
            string fileName = res[res.Length - 1];
            content.Add(new StreamContent(System.IO.File.OpenRead(drivingLicencePhoto)), "files", fileName);
            var status = await _service.UpdateProfile(appToken, content);
            await DisplayAlert("Update Profile", status ? "Profile updated successful" : "Profile update failed", "OK");
            if (status)
            {
                await Shell.Current.GoToAsync("..");
            }
            else
            {
                BtnUpdateProfile.IsEnabled = false;
                resultImage.Source = null;
            }
        }


    }
    private async void BtnPageBack_Clicked(object sender, EventArgs e)
    {
        await Shell.Current.GoToAsync($"//{nameof(HomePage)}");
    }
}
