package com.videogo.ui.others;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.appcompat.app.AlertDialog;

import com.videogo.global.ValueKeys;
import com.videogo.ui.LanDevice.LanDeviceActivity;
import com.videogo.util.DataManager;
import com.videogo.util.SpTool;
import com.videogo.widget.TitleBar;
import com.videogo.widget.testlist.TestItem;
import com.videogo.widget.testlist.TestItemAdapter;

import java.util.ArrayList;
import java.util.List;

import ezviz.ezopensdkcommon.common.RootActivity;
import ezviz.ezopensdk.R;

/**
 * 萤石功能测试页面
 * 通过配置数组来展示功能列表
 */
public class EZTestActivity extends RootActivity {

    // 特殊功能标识常量
    private static final String SPECIAL_AUTO_TEST = "SPECIAL_AUTO_TEST";
    private static final String SPECIAL_SPACE_ID = "SPECIAL_SPACE_ID";
    private static final String SPECIAL_CLEAR_VERIFY_CODE = "SPECIAL_CLEAR_VERIFY_CODE";

    private ListView listView;
    private TitleBar titleBar;
    private List<TestItem> testItems;
    private TestItemAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ez_test);
        
        initViews();
        setupDataArray();
        setupListView();
    }
    
    /**
     * 初始化视图
     */
    private void initViews() {
        titleBar = findViewById(R.id.title_bar);
        titleBar.setTitle("萤石功能测试");
        titleBar.addBackButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        
        listView = findViewById(R.id.list_view);
    }
    
    /**
     * 配置功能数组 - 参考iOS的array配置方式
     */
    private void setupDataArray() {
        testItems = new ArrayList<>();
        
        // 根据自动化测试状态显示不同标题
        String autoTestTitle = SpTool.obtainBooleanValue(ValueKeys.AUTO_TEST) 
            ? "自动化测试(开启)" : "自动化测试(关闭)";
        
        // 配置功能列表 - 格式: [标题, Activity类名或特殊功能标识]
        // Activity类名: 跳转到对应Activity
        // SPECIAL_开头: 特殊功能处理
        testItems.add(new TestItem("设备发现", "LanDeviceActivity"));
        testItems.add(new TestItem("P2P测试", "EZP2pTestActivity"));
        testItems.add(new TestItem(autoTestTitle, SPECIAL_AUTO_TEST));
        testItems.add(new TestItem("取流方式设置", "EZStreamTypeSettingActivity"));
        testItems.add(new TestItem("取流特殊设置", "EZStreamConfigSettingActivity"));
        testItems.add(new TestItem("设置云录制SpaceId", SPECIAL_SPACE_ID));
        testItems.add(new TestItem("小权限tkToken设置", "EZTKTokenActivity"));
        testItems.add(new TestItem("接口测试", "EZInterfaceTestActivity"));
        testItems.add(new TestItem("设备验证码清空", SPECIAL_CLEAR_VERIFY_CODE));
    }
    
    /**
     * 设置ListView
     */
    private void setupListView() {
        // 使用TestItemAdapter来显示主标题和副标题(类似iOS的UITableViewCellStyleSubtitle)
        adapter = new TestItemAdapter(this, testItems);
        listView.setAdapter(adapter);
        
        // 设置点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                handleItemClick(position);
            }
        });
    }
    
    /**
     * 处理列表项点击事件
     */
    private void handleItemClick(int position) {
        TestItem item = testItems.get(position);
        
        if (item.isActivityItem()) {
            // 跳转到对应的Activity
            navigateToActivity(item.getClassName(), item.getTitle());
        } else if (item.isSpecialItem()) {
            // 特殊功能处理
            handleSpecialItem(item.getClassName());
        }
    }
    
    /**
     * 跳转到指定Activity
     */
    private void navigateToActivity(String className, String title) {
        try {
            Class<?> targetClass = null;
            
            // 根据类名获取对应的Class
            switch (className) {
                case "LanDeviceActivity":
                    targetClass = LanDeviceActivity.class;
                    break;
                case "EZP2pTestActivity":
                    targetClass = EZP2pTestActivity.class;
                    break;
                case "EZStreamTypeSettingActivity":
                    targetClass = EZStreamTypeSettingActivity.class;
                    break;
                case "EZStreamConfigSettingActivity":
                    targetClass = EZStreamConfigSettingActivity.class;
                    break;
                case "EZTKTokenActivity":
                    targetClass = EZTKTokenActivity.class;
                    break;
                case "EZInterfaceTestActivity":
                    targetClass = EZInterfaceTestActivity.class;
                    break;
            }
            
            if (targetClass != null) {
                Intent intent = new Intent(this, targetClass);
                startActivity(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
            showToast("页面跳转失败: " + e.getMessage());
        }
    }
    
    /**
     * 处理特殊功能项 - 根据特殊标识符处理
     */
    private void handleSpecialItem(String specialType) {
        switch (specialType) {
            case SPECIAL_AUTO_TEST: // 自动化测试
                showAutoTestDialog();
                break;
            case SPECIAL_SPACE_ID: // 设置云录制SpaceId
                showSpaceIdDialog();
                break;
            case SPECIAL_CLEAR_VERIFY_CODE: // 设备验证码清空
                showClearVerifyCodeDialog();
                break;
        }
    }
    
    /**
     * 显示自动化测试对话框
     */
    private void showAutoTestDialog() {
        new AlertDialog.Builder(this)
            .setTitle("是否开启自动化测试")
            .setMessage("萤石SDK测试人员专用,开启后本应用中的账号登录和退出功能将禁用")
            .setPositiveButton("确定", (dialog, which) -> {
                SpTool.storeBooleanValue(ValueKeys.AUTO_TEST, true);
                // 刷新列表显示
                setupDataArray();
                adapter.updateData(testItems);
            })
            .setNegativeButton("取消", null)
            .create()
            .show();
    }
    
    /**
     * 显示SpaceId设置对话框
     */
    private void showSpaceIdDialog() {
        android.widget.EditText editText = new android.widget.EditText(this);
        editText.setHint("请输入云录制spaceId");
        
        new AlertDialog.Builder(this)
            .setTitle("请输入云录制spaceId")
            .setView(editText)
            .setPositiveButton("确定", (dialog, which) -> {
                String spaceId = editText.getText().toString();
                SpTool.storeValue(ValueKeys.SDK_CLOUD_SPACEID, spaceId);
                showToast("SpaceId设置成功");
            })
            .setNegativeButton("取消", null)
            .create()
            .show();
    }
    
    /**
     * 显示清空验证码对话框
     */
    private void showClearVerifyCodeDialog() {
        new AlertDialog.Builder(this)
            .setTitle("是否清空所有设备验证码")
            .setPositiveButton("确定", (dialog, which) -> {
                DataManager.getInstance().clearAllDeviceSerialVerifyCode();
                showToast("所有设备验证码已清空");
            })
            .setNegativeButton("取消", null)
            .create()
            .show();
    }
}
