﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using LitJson;

using System.Text;


using static UnityEngine.UI.Dropdown;
using System;

namespace VRPlayerSDK.sample
{
    [Serializable]
	public class SampleList
	{
		
		private static Sample[] GetTmpSampleList()
		{
			Sample rtmp1 = new Sample {
				MediaData = new VRPlayerSDK.PlayerMediaData {
					type = VRPlayerSDK.PlayerType.PlayerType2D_360,
					isLive = true,
					is8kfov = false,
					playURL = "rtmp://4kvrlive.hwcloudvr.cn/vrlive/huawei"
				},
				Title = "rtmp 360度全景 3D"
			};

			Sample rtmp2 = new Sample {
				MediaData = new VRPlayerSDK.PlayerMediaData {
					type = VRPlayerSDK.PlayerType.PlayerType2D_360,
					isLive = true,
					is8kfov = false,
					playURL = "rtmp://4kvrlive.hwcloudvr.cn/vrlive/huawei_4K"
				},
				Title = "rtmp 360度全景 3D 2"
			};
					
			Sample sample1 = new Sample
			{

				MediaData = new VRPlayerSDK.PlayerMediaData {
				type = VRPlayerSDK.PlayerType.PlayerType8K,
				isLive = false,
				is8kfov = true,
				//playURL = "http://192.168.1.3/88888888/16/20190813/55555559/55555559.mpd"
				//playURL = "http://192.168.1.3/88888888/16/20191126/55555597/55555597.mpd"
				playURL = "http://182.106.142.130:80/wh7f454c46tw3967251318_-1491734767/88888888/16/20191203/268437473/268437473.mpd"
				
				//playURL = "file:///sdcard/bensezhongguo_noB/index.mpd"
				},
				Title = "20 点播 8K FOV DASH"
			};

			Sample sample2 = new Sample
			{
				MediaData = new VRPlayerSDK.PlayerMediaData
				{
					type = VRPlayerSDK.PlayerType.PlayerType3D_UD,
					isLive = false,
					is8kfov = false,
					playURL = "http://152.249.4.100/88888888/16/20191119/268435537/index.m3u8"
				},
				Title = "2 点播 巨幕 3D UD HLS"
			};

			Sample sample3 = new Sample {
				MediaData = new VRPlayerSDK.PlayerMediaData {
					type = VRPlayerSDK.PlayerType.PlayerType3D_UD_360,
					isLive = false,
					is8kfov = false,
					playURL = "http://152.249.4.100/88888888/16/20191125/268435546/268435546.mp4"
				},
				Title = "3 点播 360度全景 3D UD MP4"
			};

			Sample sample4 = new Sample {
				MediaData = new VRPlayerSDK.PlayerMediaData {
					type = VRPlayerSDK.PlayerType.PlayerType3D_LR,
					isLive = false,
					is8kfov = false,
					playURL = "http://152.249.4.100/88888888/16/20191129/268435549/index.m3u8"
				},
				Title = "4 点播 巨幕 3D LR HLS"
			};

			Sample sample5 = new Sample {
				MediaData = new VRPlayerSDK.PlayerMediaData {
					type = VRPlayerSDK.PlayerType.PlayerType2D_360,
					isLive = false,
					is8kfov = false,
					playURL = "http://152.249.4.100/88888888/16/20191119/268435541/index.m3u8"
				},
				Title = "5 点播 360度全景 2D HLS"
			};			

			Sample sample52 = new Sample
			{
			   MediaData = new VRPlayerSDK.PlayerMediaData
			   {
				   type = VRPlayerSDK.PlayerType.PlayerType2D_360,
				   isLive = true,
				   is8kfov = false,
				   playURL = "rtmp://kitplay.hwcloudlive.com/live1/test01"
			   },
			   Title = "52 直播 360度全景 2D rtmp"
			};
				   
            Sample sample51 = new Sample
            {
                MediaData = new VRPlayerSDK.PlayerMediaData
                {
                    type = VRPlayerSDK.PlayerType.PlayerType2D_360,
                    isLive = true,
                    is8kfov = false,
                    playURL = "rtmp://192.168.1.3:1936/mytv/live1"
                },
                Title = "51 直播 360度全景 2D rtmp"
            };

            Sample sample6 = new Sample {
				MediaData = new VRPlayerSDK.PlayerMediaData {
					type = VRPlayerSDK.PlayerType.PlayerType3D_UD_360,
					isLive = false,
					is8kfov = false,
					playURL = "http://152.249.4.100/88888888/16/20191125/268435545/index.m3u8"
				},
				Title = "6 点播 360度全景 3D UD HLS"
			};

			Sample sample7 = new Sample {
				MediaData = new VRPlayerSDK.PlayerMediaData {
					type = VRPlayerSDK.PlayerType.PlayerType3D_LR,
					isLive = false,
					is8kfov = false,
					playURL = "http://152.249.4.100/88888888/16/20191206/268435563/index.m3u8"
				},
				Title = "7 点播 巨幕 3D LR HLS"
			};

			Sample sample8 = new Sample {
				MediaData = new VRPlayerSDK.PlayerMediaData {
					type = VRPlayerSDK.PlayerType.PlayerType2D,
					isLive = false,
					is8kfov = false,
					playURL = "http://152.249.4.100/88888888/16/20191219/268435580/index.m3u8"
				},
				Title = "8 点播 巨幕 2D HLS"
			};

			Sample sample9 = new Sample {
				MediaData = new VRPlayerSDK.PlayerMediaData {
					type = VRPlayerSDK.PlayerType.PlayerType2D,
					isLive = false,
					is8kfov = false,
					playURL = "http://152.249.4.100/88888888/16/20191119/268435531/index.m3u8"
				},
				Title = "9 点播 巨幕 2D HLS"
			};

			Sample sample20 = new Sample {
				MediaData = new VRPlayerSDK.PlayerMediaData {
					type = VRPlayerSDK.PlayerType.PlayerType8K,
					isLive = false,
					is8kfov = true,
					playURL = "http://152.249.4.100/88888888/16/20191119/268435536/268435536.mpd"
				},
				Title = "20 点播 8K FOV DASH"
			};

			Sample sample21 = new Sample {
				MediaData = new VRPlayerSDK.PlayerMediaData {
					type = VRPlayerSDK.PlayerType.PlayerType3D_LR_180,
					isLive = false,
					is8kfov = false,
					playURL = "http://152.249.4.100/88888888/16/20191205/268435561/index.m3u8"
				},
				Title = "21 点播 180 3D LR HLS"
			};
			Sample[] list = { rtmp1, rtmp2, sample1, sample2, sample3, sample4, sample5, sample52, sample51, sample6, sample7, sample8, sample9, sample20, sample21 };
			return list;
		}
 
        public static Sample[] GetLocalSampleList()
        {
            string modifiedFilePath = System.IO.Path.Combine(Application.persistentDataPath, "url.json");
            Debug.Log("before read config from file: modifiedFilePath = " + modifiedFilePath);
            if (File.Exists(modifiedFilePath))
            {
                Debug.Log("Success read config from file: modifiedFilePath = " + modifiedFilePath);
                // 规避版本前后不同的Config，会导致映射失败从而crash的问题
                try
                {
                    string modifyContent = File.ReadAllText(modifiedFilePath);
                    Debug.Log("Success read content = " + modifyContent);
                    SampleInfoList infoList = JsonMapper.ToObject< SampleInfoList>(modifyContent);
                    return infoList.sampleList;
                }
                catch (Exception e)
                {
                    Debug.Log("read error"+ e.ToString());
                    return null;
                }
            }
            return null;
        }

        public static Sample[] GetSampleList()
        {    
            Sample[] sampleList = GetLocalSampleList();
            if (sampleList != null && sampleList.Length != 0)
            {
                return sampleList;
            }
            return GetTmpSampleList();
        }
     
        public static List<OptionData> GetSampleOptionList()
		{
			Sample[] sampleList = GetSampleList();
			List<OptionData> optionList = new List<OptionData>();
			foreach(Sample i in sampleList)
			{
				optionList.Add(new OptionData(i.Title));
			}
			return optionList;
		}

		public static VRPlayerSDK.PlayerMediaData GetMediaDateByIndex(int index)
		{
			Sample[] sampleList = GetSampleList();
			return sampleList[index].MediaData;
		}

	}
	[Serializable]
	public struct Sample
	{
		public VRPlayerSDK.PlayerMediaData MediaData;
		public string Title;
	}

    public class SampleInfo
    {
        public string title;
        public int type;
        public bool isLive;
        public bool is8kfov;
        public string playURL;
    }

    public class SampleInfoList
    {
        public Sample[] sampleList;
    }

}
