using System;
using UnityEngine;
#if !UNITY_EDITOR
#if UNITY_IOS
using System.Runtime.InteropServices;
#else
using System.IO;
#endif
#endif

// 使用方法
// ShareManagerという名前のオブジェクトに下記コンポーネントをアタッチしてShare~~を呼び出す
public class ShareManager : SingletonMonoBehaviour<ShareManager> {

#if !UNITY_EDITOR
    private Action<bool> callback;
#if UNITY_IOS
#else
    private string TexturePath = "";

    private AndroidJavaObject worker = null;
#endif
#endif


    void Awake(){
        if(CheckInstance()) return ;
#if !UNITY_EDITOR
#if UNITY_IOS
#else
        TexturePath = Application.persistentDataPath + "/SNSTexture.png";
        worker = new AndroidJavaObject("jp.usaya.lib.UsayaUnityPlayerActivity");
#endif
#endif
    }

    public void ShareTwitter(string message, Texture2D texture, Action<bool> callback){
#if !UNITY_EDITOR
        this.callback = callback;
#if UNITY_IOS
        if(NativeUtils.GetiOSMajorVersion() >= 11){
            if(NativeUtils.canOpenTwitter()){
                callback(true);
                PostTwitter(message, texture);
            }else{
                callback(false);
            }
            this.callback = ActionExtensions.EmptyActionBool;
        }else{
            PostTwitter(message, texture);
        }
#else
        SaveTexture(texture, TexturePath);
        PostTwitter(message, TexturePath);
#endif
#else
        callback(true);
#endif
    }
#if UNITY_ANDROID
    public void ShareTwitter(string message, string path, Action<bool> callback){
#if !UNITY_EDITOR
        this.callback = callback;
        PostTwitter(message, path);
#else
        callback(true);
#endif
    }
#endif

    public void ShareFacebook(string message, Texture2D texture, Action<bool> callback){
#if !UNITY_EDITOR
        this.callback = callback;
#if UNITY_IOS
        if(NativeUtils.GetiOSMajorVersion() >= 11){
            if(NativeUtils.canOpenFacebook()){
                callback(true);
                PostFacebook(message, texture);
            }else{
                callback(false);
            }
            this.callback = ActionExtensions.EmptyActionBool;
        }else{
            PostFacebook(message, texture);
        }
#else
        SaveTexture(texture, TexturePath);
        PostFacebook(TexturePath);
#endif
#else
        callback(true);
#endif
    }
#if UNITY_ANDROID
    public void ShareFacebook(string message, string path, Action<bool> callback){
#if !UNITY_EDITOR
        this.callback = callback;
        PostFacebook(path);
#else
        callback(true);
#endif
    }
#endif

#region iOS 
#if (UNITY_IOS && !UNITY_EDITOR)
/*
    public static void WhatsappShare(string message, Texture2D texture){
        if(texture == null){
            Instance.WhatsAppShareText(message);
        }else{
            Instance.WhatsAppShareImage(texture);
        }
    }
*/
    [DllImport("__Internal")]
    private static extern void _MSP_TwPost(string text, string url, string encodedMedia);

    [DllImport("__Internal")]
    private static extern void _MSP_FbPost(string text, string url, string encodedMedia);
/*
    [DllImport("__Internal")]
    private static extern void _MSP_MediaShare(string text, string encodedMedia);
    
    [DllImport("__Internal")]
    private static extern void _ISN_WP_ShareText(string message);

    [DllImport("__Internal")]
    private static extern void _ISN_WP_ShareMedia(string encodedMedia);

    public void ShareMedia(string text, Texture2D texture){
        if(texture != null){
            byte[] val = texture.EncodeToPNG();
            string bytesString = System.Convert.ToBase64String(val);
            _MSP_MediaShare(text, bytesString);
        }else{
            _MSP_MediaShare(text, "");
        }
    }
*/

    private void PostTwitter(string text, Texture2D texture){
        if(text == null) text = "";

        if(texture != null){
            byte[] val = texture.EncodeToPNG();
            string encodedMedia = System.Convert.ToBase64String(val);
            _MSP_TwPost(text, "", encodedMedia);
        }else{
            _MSP_TwPost(text, "", "");
        }
    }

    private void PostFacebook(string text, Texture2D texture){
        if(text == null) text = "";
        
        if(texture != null){
            byte[] val = texture.EncodeToPNG();
            string encodedMedia = System.Convert.ToBase64String(val);
            _MSP_FbPost(text, "", encodedMedia);
        }else{
            _MSP_FbPost(text, "", "");
        }
    }
/*
    private void WhatsAppShareText(string message){
        _ISN_WP_ShareText(message);
    }
    private void WhatsAppShareImage(Texture2D texture){
        byte[] val = texture.EncodeToPNG();
        string bytesString = System.Convert.ToBase64String(val);

        _ISN_WP_ShareMedia(bytesString);
    }
*/

    // callbacks
    private void OnTwitterPostFailed(){
        callback(false);
    }
    private void OnTwitterPostSuccess(){
        callback(true);
    }
    private void OnFacebookPostFailed(){
        callback(false);
    }
    private void OnFacebookPostSuccess(){
        callback(true);
    }
#endif
#endregion

#region Android
#if (UNITY_ANDROID && !UNITY_EDITOR)

    private void SaveTexture(Texture2D texture, string path){
        using(FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write)){
            using(BinaryWriter writer = new BinaryWriter(fileStream)){
                writer.Write(texture.EncodeToPNG());
            }
        }
    }

    private void PostTwitter(string message, string imagePath){
        if(message == null)message = "";
        if(imagePath == null)imagePath = "";
        worker.Call("postTwitterOrFacebook", true, message, "", imagePath);
    }

    private void PostFacebook(string imagePath){
        if(imagePath == null)imagePath = "";
        worker.Call("postTwitterOrFacebook", false, "", "", imagePath);
    }

    private void CreateChooser(string message, string imagePath){
        if(message == null)message = "";
        if(imagePath == null)imagePath = "";
        worker.Call("createChooser", message, imagePath);
    }

    private void OnShareManagerResult(string result){
        switch(result){
        case "0":
            callback(true);
            break;
        default:
            callback(false);
            break;
        }
    }
#endif
#endregion
}
