using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SUIPanelMobile : SUIObjectBase
{
    GameObject _panelMobile;

    //Panel_Control
    GameObject _panelControl;
    GameObject _buttonVOD;
    GameObject _buttonList;

    //Panel_Content
    GameObject _panelContent;
    GameObject _panelVOD;
    GameObject _inputField;
    GameObject _buttonPlay;
    GameObject _panelList;
    GameObject _panelWebView;
    GameObject _imageWeb;

    public override void UI_Awake()
    {
        GameObject rootCanvas;
        rootCanvas = GameObject.Find("Canvas");
        Debug.Assert(rootCanvas != null);

        _rootPanel = rootCanvas.transform.Find("Panel_Mobile").gameObject;
        Debug.Assert(_rootPanel != null);

        _panelControl = _rootPanel.transform.Find("Panel_Control").gameObject;
        Debug.Assert(_panelControl != null);

        _buttonVOD = _panelControl.transform.Find("Button_VOD").gameObject;
        Debug.Assert(_buttonVOD != null);

        _buttonList = _panelControl.transform.Find("Button_List").gameObject;
        Debug.Assert(_buttonList != null);

        _panelContent = _rootPanel.transform.Find("Panel_Content").gameObject;
        Debug.Assert(_panelContent != null);

        _panelVOD = _panelContent.transform.Find("Panel_VOD").gameObject;
        Debug.Assert(_panelVOD != null);

        _inputField = _panelVOD.transform.Find("InputField").gameObject;
        Debug.Assert(_inputField != null);

        _buttonPlay = _panelVOD.transform.Find("Button_Play").gameObject;
        Debug.Assert(_buttonPlay != null);

        _panelList = _panelContent.transform.Find("Panel_List").gameObject;
        Debug.Assert(_panelList != null);

        _panelWebView = _panelList.transform.Find("Panel_WebView").gameObject;
        Debug.Assert(_panelWebView != null);

        _imageWeb = _panelWebView.transform.Find("Image_Web").gameObject;
        Debug.Assert(_imageWeb != null);
    }

    public override void UI_BindTouchEvent()
    {
        BindButtonClick("Panel_Control/Button_VOD", onClickButtonVOD);
        BindButtonClick("Panel_Control/Button_List", onClickButtonList);
        BindButtonClick("Panel_Content/Panel_VOD/Button_Play", onClickButtonPlay);
    }

    private void onClickButtonVOD(GameObject buttonObject)
    {
        Debug.Log("SUIPanelMobile -> onClickButtonVOD");
    }

    private void onClickButtonList(GameObject buttonObject)
    {
        Debug.Log("SUIPanelMobile -> onClickButtonList");

        _panelList.SetActive(true);
        _panelVOD.SetActive(false);

        var webViewGameObject = new GameObject("UniWebView");
        webViewGameObject.transform.position = _panelWebView.transform.position;
        webViewGameObject.transform.SetParent(GameObject.Find("Panel_Content/Panel_List/Panel_WebView/Image_Web").transform);
        
        UniWebView webView = webViewGameObject.AddComponent<UniWebView>();
        webView.Frame = new Rect(0, 0, Screen.width, Screen.height);
        Debug.Log("screen width -->" + Screen.width);
        Debug.Log("screen height -->" + Screen.height);
        //webView.Frame = new Rect(0, 0, 1000f, 100f);
        webView.Load("https://www.baidu.com");
        
        webView.Show();
        webView.UpdateFrame();
    }

    private void onClickButtonPlay(GameObject buttonObject)
    {
        Debug.Log("SUIPanelMobile -> onClickButtonPlay");

        string url = _inputField.GetComponent<InputField>().text;
        Debug.Log("url---->" + url);
        SLogicManager logicManager = GetComponent<SLogicManager>();
        logicManager.setVideoUrl(url);

        UnityEngine.SceneManagement.SceneManager.LoadScene("Mobile_Video");
    }
}
