using Newtonsoft.Json;
using REST_JSON_API;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;

/// <summary>
/// Point in 3D Space
/// </summary>
public class PyramidFact : FactWrappedCRTP<PyramidFact>
{


    //used points
    public string PidR, PidD;

    public Vector3 A, B, C, M, D, D_offset;

    public float ab;
    public float bc;

    public float Volume = 0.0F;



    public RectangleFact GetR {get =>  (RectangleFact)FactRecorder.AllFacts[PidR];}
    public PointFact GetD {get =>  (PointFact)FactRecorder.AllFacts[PidD];}
    protected void calculate_vectors(){

        A = GetR.A + Vector3.zero;
        B = GetR.B + Vector3.zero;
        C = GetR.C + Vector3.zero;
        D = GetD.Point + Vector3.zero;

        D_offset = Quaternion.AngleAxis(-90, Vector3.right) * (D - B);

        /*
        Vector3 d_AB = A + Vector3.Project(D - A, B - A);
        Vector3 d_BC = B + Vector3.Project(D - B, C - B);

        abPosition = Vector3.Distance(B, d_AB) / Vector3.Distance(B, A);
        bcPosition = Vector3.Distance(B, d_BC) / Vector3.Distance(B, C);
        */

        ab = (B - A).magnitude;
        bc = (C - B).magnitude;

        M = Vector3.ProjectOnPlane(D, Vector3.Cross(A - B, C - B));

        Volume = 1/3 * GetR.Area * Vector3.Distance(M, D);

        Position = B;

        Vector3 forward = Vector3.Cross((C - B), (A - B));
        Vector3 up = A - B;

        if ((A - B).y < 0)
        {
            up = -up;
        }

        Rotation = Quaternion.LookRotation(forward, up);
    }

    public PyramidFact() : base(){
        this.PidR = null;
        this.PidD = null;
    }
    [JsonConstructor]
    public PyramidFact( string PidT, string PidD) : base()
    {
        this.PidR = PidT;
        this.PidD = PidD;

        calculate_vectors();
    }

    /// <summary>
    /// Bypasses initialization of new MMT %Fact by using existend URI, _which is not checked for existence_.
    /// <see cref="Normal"/> set to <c>Vector3.up</c>
    /// </summary>
    /// <param name="Point">sets <see cref="Point"/></param>
    /// <param name="ServerDefinition">MMT URI as OMS</param>
    public PyramidFact(string PidR, string PidD, SOMDoc ServerDefinition) : base()
    {
        this.PidR = PidR;
        this.PidD = PidD;
        
        this.ServerDefinition = ServerDefinition;

        calculate_vectors();
    }

    /// \copydoc Fact.parseFact(ScrollFact)
    public new static IEnumerator parseFact(List<Fact> ret, MMTFact fact)
    {
        if (((MMTGeneralFact)fact).defines is not OMA df)
            yield break;

        OMS rectangleR, pointD;

        rectangleR = (OMS)df.arguments[0];
        pointD = (OMS)df.arguments[1];

        string PidR = rectangleR.uri;
        string PidD = pointD.uri;

        

        ret.Add(new PyramidFact(PidR, PidD));

        //ParsingDictionary.parseTermsToId.TryAdd(defines.ToString(), fact.@ref.uri);
        //ret.Add(new PointFact(SOMDoc.MakeVector3(defines), fact.@ref));
    }


    /// \copydoc Fact.hasDependentFacts
    public override bool HasDependentFacts => true;

    /// \copydoc Fact.getDependentFactIds
    protected override string[] GetDependentFactIds()
        => new string[] { PidR, PidD};

    /// \copydoc Fact.GetHashCode
    /* public override int GetHashCode()
        => this.Point.GetHashCode();
    */
    protected override void RecalculateTransform()
    {
        calculate_vectors();
    }
    /// \copydoc Fact.Equivalent(Fact, Fact)
    protected override bool EquivalentWrapped(PyramidFact p1, PyramidFact p2){
        return DependentFactsEquivalent(p1, p2);
    }

    protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new){
        return new PyramidFact(this.PidR, this.PidD);
    }

    public override MMTFact MakeMMTDeclaration()
    {
        SOMDoc tp = new OMS(MMTConstants.PyramidType);

        return new MMTGeneralFact(_LastLabel, tp, Defines());
    }

    public override SOMDoc Defines()
        => new OMA(
                new OMS(MMTConstants.PyramidCons),
                new[] {
                    new OMS(PidR),
                    new OMS(PidD),
                }
            );
}