using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NBC;
// using Obi;
using UnityEngine;

namespace NBF
{
    public class FLine : FGearBase
    {
        // [SerializeField] private ObiParticleAttachment startParticleAttachment;
        [SerializeField] private bool isLureConnect;
        [SerializeField] private RodLine rodLine;
        [SerializeField] private Rope fishingRope;
        [SerializeField] private Rope bobberRope;
        public LureController Lure;
        public BobberController Bobber;


        private float _groundSetting = 0.5f;

        private float _LineOnSpool = 100f;

        private float _LineThickness = 0.0007f;

        public bool IsLure => isLureConnect;

        // public event Action OnLinePulled;

        protected override void OnInit()
        {
            var tipRb = Rod.Asset.LineConnectorRigidbody;
            if (isLureConnect)
            {
                Lure.SetJoint(tipRb);
                Lure.EnableCollision(false);
            }
            else
            {
                fishingRope.startAnchor = tipRb;
                Bobber.SetJoint(tipRb);
                Lure.SetJoint(Bobber.rbody);
                Lure.gameObject.SetActive(true);
                Lure.EnableCollision(false);
                Lure.SetKinematic(false);
            }

            GetComponentsInChildren<Transform>(includeInactive: true).ToList().ForEach(delegate(Transform i)
            {
                i.gameObject.SetActive(true);
            });

            StartCoroutine(LureUseGravity());
            if (isLureConnect)
            {
                fishingRope.Init(Rod);
            }
            else
            {
                fishingRope.Init(Rod);
                bobberRope.Init(Rod);
            }

            // rodLine.GenerateLineRendererRope(guides.ToArray(), _LineThickness);
        }

        public void InitTest(Rigidbody tipRb)
        {
            if (isLureConnect)
            {
                Lure.SetJoint(tipRb);
                Lure.EnableCollision(false);
            }
            else
            {
                fishingRope.startAnchor = tipRb;
                Bobber.SetJoint(tipRb);
                Lure.SetJoint(Bobber.rbody);
                Lure.gameObject.SetActive(true);
                Lure.EnableCollision(false);
                Lure.SetKinematic(false);
            }

            GetComponentsInChildren<Transform>(includeInactive: true).ToList().ForEach(delegate(Transform i)
            {
                i.gameObject.SetActive(true);
            });

            StartCoroutine(LureUseGravity());
            if (isLureConnect)
            {
                fishingRope.Init(Rod);
            }
            else
            {
                fishingRope.Init(Rod);
                bobberRope.Init(Rod);
            }
        }

        private IEnumerator LureUseGravity()
        {
            yield return 1;
            Lure.gameObject.SetActive(false);
            Lure.gameObject.SetActive(true);
            yield return 1;
            Lure.RBody.useGravity = true;
        }


        public void EnableLineRenderers()
        {
            // foreach (ObiRopeExtrudedRenderer item in GetComponentsInChildren<ObiRopeExtrudedRenderer>().ToList())
            // {
            //     item.enabled = true;
            // }
        }

        public void SetObiRopeStretch(float value)
        {
            Log.Error($"SetObiRopeStretch={value}");
            fishingRope.SetTargetLength(value - 0.2f);
        }
    }
}