package com.baijia.live.app;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.baijiayun.live.ui.LiveSDKWithUI;
import com.baijiayun.livecore.LiveSDK;
import com.baijiayun.livecore.context.LPConstants;
import com.baijiayun.livecore.context.LiveRoom;
import com.baijiayun.livecore.models.LPJoinCodeEnterRoomModel;
import com.baijiayun.livecore.models.LPSignEnterRoomModel;
import com.baijiayun.livecore.models.LPUserModel;
import com.baijiayun.livecore.utils.LPLogger;
import com.baijiayun.liveuibase.listeners.LPRoomListener;

/**
 * Created by Shubo on 2017/3/20.
 */

public class EntryActivity extends AppCompatActivity {

    private SharedPreferences sp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entry);

        sp = getSharedPreferences("live_temp", Context.MODE_PRIVATE);
        //data
        String code1 = sp.getString("code", "tbsmjz");
        String domain = sp.getString("domain", null);

        ((EditText) findViewById(R.id.activity_entry_join_code)).setText(code1);
        if (!TextUtils.isEmpty(domain))
            ((EditText) findViewById(R.id.activity_entry_customer_domain)).setText(domain);

        findViewById(R.id.enter).setOnClickListener(v -> {
//            enterRoom();
            String code = ((EditText) findViewById(R.id.activity_entry_join_code)).getText().toString();
            String name = ((EditText) findViewById(R.id.activity_entry_name)).getText().toString();
            String domain1 = ((EditText) findViewById(R.id.activity_entry_customer_domain)).getText().toString();
            LiveSDK.customEnvironmentPrefix = domain1;

            SharedPreferences.Editor editor = sp.edit();
            editor.putString("code", code);
            editor.putString("domain", domain1);
            editor.apply();
            LiveSDKWithUI.enterRoom(EntryActivity.this, new LPJoinCodeEnterRoomModel(code, name));
        });

        ((RadioGroup) findViewById(R.id.rg_env)).setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rg_env_product) {
                LiveSDK.deployType = LPConstants.LPDeployType.Product;
            } else if (checkedId == R.id.rg_env_beta) {
                LiveSDK.deployType = LPConstants.LPDeployType.Beta;
            } else {
                LiveSDK.deployType = LPConstants.LPDeployType.Test;
            }
        });

        LiveSDKWithUI.setCustomPageListener(new LPRoomListener.LPRoomCustomPageListener() {
            @Override
            public void onOpenCustomPage(LiveRoom liveRoom, String url) {
                LPLogger.d("onOpenCustomPage");
            }

            @Override
            public void onCloseCustomPage() {
                // do nothing
                LPLogger.d("onCloseCustomPage");
            }
        });
    }

    private void enterRoom(){
        LPUserModel userModel = new LPUserModel();
        userModel.name = "技术支持";
        userModel.number = "222";
        userModel.type = LPConstants.LPUserType.Student;
        LPSignEnterRoomModel lpSignEnterRoomModel = new LPSignEnterRoomModel(19080856440470L, userModel, "40679a0db16f8abd993ffd8beab88776");
        LiveSDKWithUI.enterRoom(EntryActivity.this, lpSignEnterRoomModel);
    }
}
