package com.meeting.demo.view.sdk;


import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Switch;

import androidx.annotation.NonNull;

import com.huawei.hwmsdk.enums.NetworkAccessType;
import com.meeting.demo.R;
import com.meeting.demo.constant.ConfigConstant;
import com.meeting.demo.dependency.NeedCustomType;
import com.meeting.demo.utils.DemoUtil;
import com.meeting.demo.view.base.BaseDialogFragment;


public class ServerSettingFragment extends BaseDialogFragment {
    public final static String TAG = ServerSettingFragment.class.getSimpleName();

    private EditText mEtCustomSiteAddress;

    private EditText mEtCustomSitePort;

    private RadioGroup mRgProxyType;

    private Switch mSwitchCaCheck;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (getDialog() != null) {
            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        View vRoot = inflater.inflate(R.layout.server_setting_layout, container, false);
        mEtCustomSiteAddress = vRoot.findViewById(R.id.server_address_input);
        mEtCustomSitePort = vRoot.findViewById(R.id.server_port_input);
        mRgProxyType = vRoot.findViewById(R.id.rg_proxy_type);
        mSwitchCaCheck = vRoot.findViewById(R.id.checked_ssl);
        initViews();
        vRoot.findViewById(R.id.save_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleSaveButtonClicked();
            }
        });
        return vRoot;
    }

    private void initViews() {
        mEtCustomSiteAddress.setText(TextUtils.isEmpty(DemoUtil.getCustomSiteAddress()) ? "" :
                DemoUtil.getCustomSiteAddress());
        String customServerPort = String.valueOf(DemoUtil.getCustomSitePort());
        mEtCustomSitePort.setText(TextUtils.equals(customServerPort, "0") ? ConfigConstant.COMMON_SERVER_PORT :
                customServerPort);
        mSwitchCaCheck.setChecked(DemoUtil.needCustom(NeedCustomType.SITE_CA_CHECK, true));
        mSwitchCaCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DemoUtil.setCustom(NeedCustomType.SITE_CA_CHECK, isChecked);
            }
        });
        NetworkAccessType customSiteProxy = DemoUtil.getCustomSiteProxy();
        if (customSiteProxy == NetworkAccessType.ACCESS_TYPE_INNER) {
            mRgProxyType.check(R.id.rb_proxy_inner);
        } else if (customSiteProxy == NetworkAccessType.ACCESS_TYPE_OUTER) {
            mRgProxyType.check(R.id.rb_proxy_outer);
        } else {
            mRgProxyType.check(R.id.rb_proxy_auto);
        }
    }

    private void handleSaveButtonClicked() {
        String customSiteAddress = mEtCustomSiteAddress.getText().toString().trim();
        String customSitePort = mEtCustomSitePort.getText().toString().trim();
        if (TextUtils.isEmpty(customSiteAddress) || TextUtils.isEmpty(customSitePort) || TextUtils.equals("0",
                customSitePort)) {
            DemoUtil.showToast("未输入有效地址/端口号");
            return;
        } else {
            DemoUtil.showToast("应用进程重启后地址和端口修改生效");
            DemoUtil.setCustom(NeedCustomType.SITE_CA_CHECK, mSwitchCaCheck.isChecked());
            DemoUtil.setCustomSiteAddress(customSiteAddress);
            DemoUtil.setCustomSitePort(Integer.parseInt(customSitePort));
            NetworkAccessType customSiteProxy;
            int checkedRadioButtonId = mRgProxyType.getCheckedRadioButtonId();
            if (checkedRadioButtonId == R.id.rb_proxy_inner) {
                customSiteProxy = NetworkAccessType.ACCESS_TYPE_INNER;
            } else if (checkedRadioButtonId == R.id.rb_proxy_outer) {
                customSiteProxy = NetworkAccessType.ACCESS_TYPE_OUTER;
            } else {
                customSiteProxy = NetworkAccessType.ACCESS_TYPE_AUTO;
            }
            DemoUtil.setCustomSiteProxy(customSiteProxy);
        }
        dismiss();
        showLoading();
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            dismissLoading();
            DemoUtil.restartApplication();
        }, 1500L);
    }
}
