// マイクデバイス設定の情報を管理/保持する存在
// "データの管理/保持"だけをさせる事。加工はさせない。
class MicPropertyModel{
    const string MICPROPERTY_COOKIE_NAME_DONT_SHOWAGAIN = "dontShowAgain";
    const string MICPROPERTY_COOKIE_NAME_SELECT_MIC_DEVICEID = "selectMicDeviceID";

    bool _isDoNotShowAgain;
    bool _isMicPermissionAllow;


    string _deviceIDCache;

    int _currentMicDeviceIndex;

    list<string> _deviceIDList;
    list<string> _deviceNameList;

    public MicPropertyModel(){
        _currentMicDeviceIndex = -1;
        _isMicPermissionAllow = false;

        SetDontShowAgainFlag(hsCookieGetBool(MICPROPERTY_COOKIE_NAME_DONT_SHOWAGAIN));
        SetMicDeviceIDChache(hsCookieGetStr(MICPROPERTY_COOKIE_NAME_SELECT_MIC_DEVICEID));

        _deviceIDList = new list<string>();
        _deviceNameList = new list<string>();
    }

    public void SetDontShowAgainFlag(bool showFlag){
        _isDoNotShowAgain = showFlag;
    }

    public bool GetDontShowAgainFlag(){
        return _isDoNotShowAgain;
    }

    private void SetMicDeviceIDChache(string deviceID){
        _deviceIDCache = deviceID;
    }

    public string GetMicDeviceIDChache(){
        return _deviceIDCache;
    }

    public int GetCurrentMicDeviceIndex(){
        return _currentMicDeviceIndex;
    }

    public void SetMicPermission(bool isAllow){
        _isMicPermissionAllow = isAllow;
    }

    public bool GetMicPermission(){
        return _isMicPermissionAllow;
    }

    // 現在選択しているマイクデバイスのIndexを取得
    public string GetCurrentMicDeviceID(){
        string deviceID = "not Found";
        if(_currentMicDeviceIndex != -1 && IsMicDeviceListEmpty() == false){
            deviceID = _deviceIDList[_currentMicDeviceIndex];
        }

        return deviceID;
    }

    // 選択中のIndexからマイクデバイスの名前を取得
    public string GetCurrentMicDeviceName(){
        string deviceName = "not Found";
        if(_currentMicDeviceIndex != -1 && IsMicDeviceListEmpty() == false){
            deviceName = _deviceNameList[_currentMicDeviceIndex];
        }

        return deviceName;
    }

    // マイクデバイスの名前一覧
    public list<string> GetMicDeviceNameList(){
        return _deviceNameList;
    }

    // hel_net_get_mic_device_listで受信したJsVal構造体からデバイスのIDと名前リストを生成
    public void CreateDeviceList(JsVal micDeviceListData){
        // リストはクリアする
        _deviceIDList = new list<string>();
        _deviceNameList = new list<string>();

        if (micDeviceListData.IsNull())
        {
            // マイク未許可の場合
            return;
        }

        list<JsProp> deviceList = micDeviceListData.GetPropertyList();
        for(int i = 0; i < deviceList.Count(); i++){
            JsVal deviceData = deviceList[i].GetValue();
            string deviceID = deviceData.GetProperty("deviceId").ToString();

            // ID登録
            deviceID = deviceID.SubString(1, deviceID.Length()-2);
            _deviceIDList.Add(deviceID);

            // 名前登録
            string deviceName = deviceData.GetProperty("label").ToString("");
            deviceName = deviceName.SubString(1, deviceName.Length()-2);
            _deviceNameList.Add(deviceName);
        }
    }

    // hel_net_get_current_mic_deviceで取得した「現在選択しているデバイス情報」を展開
    public void CreateCurrentDevice(JsVal currentDeviceData){
        if (currentDeviceData.IsNull())
        {
            _currentMicDeviceIndex = -1;
            return;
        }

        string micDeviceID = currentDeviceData.GetProperty("deviceId").ToString();
        micDeviceID = micDeviceID.SubString(1, micDeviceID.Length()-2);

        _currentMicDeviceIndex = FindMicDeviceIndexByDeviceID(micDeviceID);
    }

    // 選択しているマイクデバイスIndecを変更(セーフティ付き)
    public void SetCurrentMicDeviceID(int index){
        if(index < 0){
            index = 0;
        }else if(index >= _deviceIDList.Count()){
            index = _deviceIDList.Count() - 1;
        }

        _currentMicDeviceIndex = index;
    }

    // 「次のマイクをテスト」ボタンでIndexを次に進める。（終端まで行ったらループさせる）
    public void NextMicDevice(){
        _currentMicDeviceIndex++ ;
        _currentMicDeviceIndex = _currentMicDeviceIndex % _deviceIDList.Count();
    }

    // デバイスID(string)と一致するデバイス名を取得
    public int FindMicDeviceIndexByDeviceID(string deviceID){
        int findIndex = -1;
        for(int i = 0; i < _deviceIDList.Count(); i++){
            if(_deviceIDList[i] == deviceID){
                findIndex = i;
                break;
            }
        }

        return findIndex;
    }

    // "次から表示しない"の選択情報をcookieに登録
    public void SetShowAgeinCookie(){
        hsCookieSetBool(MICPROPERTY_COOKIE_NAME_DONT_SHOWAGAIN, _isDoNotShowAgain);
    }

    // マイクデバイスIDをcookieに登録
    public void SetSelectMicDeviceIDCookie(string micDeviceID){
        hsCookieSetStr(MICPROPERTY_COOKIE_NAME_SELECT_MIC_DEVICEID, micDeviceID);
    }

    // マイクデバイスが検出できたかのチェッカー
    public bool IsMicDeviceListEmpty(){
        return _deviceIDList.Count() <= 0;
    }

}
