//
//  UniWebViewMessage.cs
//  Created by Wang Wei(@onevcat) on 2017-05-12.
//
//  This file is a part of UniWebView Project (https://uniwebview.com)
//  By purchasing the asset, you are allowed to use this code in as many as projects 
//  you want, only if you publish the final products under the name of the same account
//  used for the purchase. 
//
//  This asset and all corresponding files (such as source code) are provided on an 
//  “as is” basis, without warranty of any kind, express of implied, including but not 
//  limited to the warranties of merchantability, fitness for a particular purpose, and 
//  noninfringement. In no event shall the authors or copyright holders be liable for any 
//  claim, damages or other liability, whether in action of contract, tort or otherwise, 
//  arising from, out of or in connection with the software or the use of other dealing in the software.
//
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
#if UNITY_2017_3_OR_NEWER
using Net = UnityEngine.Networking.UnityWebRequest;
#else
using Net = UnityEngine.WWW;
#endif
/// 
/// A structure represents a message from webview.
/// 
public struct UniWebViewMessage {
    /// 
    /// Gets the raw message. It is the original url which initialized this message.
    /// 
    public string RawMessage {get; private set;}
    /// 
    /// The url scheme of this UniWebViewMessage. "uniwebview" was added to message scheme list
    /// by default. You can add your own scheme by using `UniWebView.AddUrlScheme`.
    /// 
    public string Scheme {get; private set;}
    /// 
    /// The path of this UniWebViewMessage.
    /// This will be the decoded value for the path of original url.
    /// 
    public string Path {get; private set;}
    /// 
    /// The arguments of this UniWebViewMessage.
    ///
    /// When received url "uniwebview://yourPath?param1=value1¶m2=value2", 
    /// the args is a Dictionary with: Args["param1"] = value1, Args["param2"] = value2
    /// 
    /// Both the key and valud will be url decoded from the original url.
    /// 
    public Dictionary Args{get; private set;}
    /// 
    /// Initializes a new instance of the `UniWebViewMessage` struct.
    /// 
    /// Raw message which will be parsed to a UniWebViewMessage.
    public UniWebViewMessage(string rawMessage): this() {
        UniWebViewLogger.Instance.Debug("Try to parse raw message: " + rawMessage);
        this.RawMessage = rawMessage;
        
        string[] schemeSplit = rawMessage.Split(new string[] {"://"}, System.StringSplitOptions.None);
        if (schemeSplit.Length == 1) {
            // `://` not existing. Try `:/` instead.
            schemeSplit = rawMessage.Split(new string[] {":/"}, System.StringSplitOptions.None);
        }
        if (schemeSplit.Length == 1) {
            // `:/` not existing. Try `:` instead.
            schemeSplit = rawMessage.Split(new string[] {":"}, System.StringSplitOptions.None);
        }
        if (schemeSplit.Length >= 2) {
            this.Scheme = schemeSplit[0];
            UniWebViewLogger.Instance.Debug("Get scheme: " + this.Scheme);
            string pathAndArgsString = "";
            int index = 1;
            while (index < schemeSplit.Length) {
                pathAndArgsString = string.Concat(pathAndArgsString, schemeSplit[index]);
                index++;
            }
            UniWebViewLogger.Instance.Verbose("Build path and args string: " + pathAndArgsString);
            
            string[] split = pathAndArgsString.Split("?"[0]);
            
            this.Path = Net.UnEscapeURL(split[0].TrimEnd('/'));
            this.Args = new Dictionary();
            if (split.Length > 1) {
                foreach (string pair in split[1].Split("&"[0])) {
                    string[] elems = pair.Split("="[0]);
                    if (elems.Length > 1) {
                        var key = Net.UnEscapeURL(elems[0]);
                        if (Args.ContainsKey(key)) {
                            var existingValue = Args[key];
                            Args[key] = existingValue + "," + Net.UnEscapeURL(elems[1]);
                        } else {
                            Args[key] = Net.UnEscapeURL(elems[1]);
                        }
                        UniWebViewLogger.Instance.Debug("Get arg, key: " + key + " value: " + Args[key]);
                    }
                }
            }
        } else {
            UniWebViewLogger.Instance.Critical("Bad url scheme. Can not be parsed to UniWebViewMessage: " + rawMessage);
        }
    }
}