package com.genlot.ninglottery.module.function.settings;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.genlot.nativesdk.SystemNI;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseFragment;
import com.genlot.ninglottery.game.GameManager;
import com.genlot.ninglottery.model.Constant;
import com.genlot.ninglottery.toolkit.SharedUtil;

import java.util.List;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 系统参数设置
 * @date 2019/9/24
 */
public class SystemParamsFragment extends BaseFragment {

    String ipFormat = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
            +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
            +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
            +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
    private String[] mCurrentIP = new String[4];
    private int[] mCurrentPort = new int[4];

    @OnClick(R.id.bt_save)
    public void saveSettings() {
        boolean saveFlag = false;
        String station = etStation.getText().toString().trim();
        String trainMode = tvSwitch.getText().toString().trim();
        if (TextUtils.isEmpty(station)) {
            etStation.setError("站点号不能为空!");
            return;
        }
        if (!station.equals(String.valueOf(SystemNI.getTerminalID()))) {
            saveFlag = true;
            SystemNI.setTerminalID(Long.parseLong(station));
        }
        if (trainMode.equals("关") && SharedUtil.getBoolean(Constant.KEY_TRAIN_MODE, false)) {
            saveFlag = true;
            SharedUtil.putBoolean(Constant.KEY_TRAIN_MODE, false);
            GameManager.isTrainMode = false;
        }
        if ("开".equals(trainMode) && !SharedUtil.getBoolean(Constant.KEY_TRAIN_MODE, false)) {
            saveFlag = true;
            SharedUtil.putBoolean(Constant.KEY_TRAIN_MODE, true);
            GameManager.isTrainMode = true;
        }
        String[] IPs = new String[4];
        String[] Ports = new String[4];
        Pattern pattern = Pattern.compile(ipFormat);
        for(int i = 0;i<4;i++) {
            IPs[i] = mIPList.get(i).getText().toString().trim();
            if (!pattern.matcher(IPs[i]).matches() && !TextUtils.isEmpty(IPs[i])) {
                mIPList.get(i).setError("IP" + (i+1) + "格式不对");
                return;
            }
            Ports[i] = mPortList.get(i).getText().toString().trim();
            if (TextUtils.isEmpty(Ports[i])) {
                mPortList.get(i).setText("0");
            }
            if (!Pattern.compile("[0-9]*").matcher(Ports[i]).matches()) {
                mPortList.get(i).setError("PORT" + (i+1) + "格式不对");
                return;
            }
            if (!IPs[i].equals(mCurrentIP[i]) || !Ports[i].equals(String.valueOf(mCurrentPort[i]))) {
                SystemNI.setCommLine(i, IPs[i], Integer.parseInt(Ports[i]));
                saveFlag = true;
            }
        }
        if (saveFlag) {
            ToastUtils.showShort("保存成功!");
        } else {
            ToastUtils.showShort("没有变化!");
        }
    }

    @OnClick(R.id.tv_switch)
    public void changeSwitch() {
        String text = tvSwitch.getText().toString().trim();
        if ("关".equals(text)) {
            tvSwitch.setText("开");
        }
        if ("开".equals(text)) {
            tvSwitch.setText("关");
        }
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_system_params;
    }

    @Override
    public void onCreateViewFinished(Bundle state) {
        etStation.setText(String.valueOf(SystemNI.getTerminalID()));
        for(int i = 0;i<4;i++) {
            mCurrentIP[i] = SystemNI.getCommIP(i);
            mCurrentPort[i] = SystemNI.getCommPort(i);
            mIPList.get(i).setText(mCurrentIP[i]);
            mPortList.get(i).setText(String.valueOf(mCurrentPort[i]));
        }
        if (SharedUtil.getBoolean(Constant.KEY_TRAIN_MODE, false)) {
            tvSwitch.setText("开");
        } else {
            tvSwitch.setText("关");
        }
    }

    @BindView(R.id.et_station)
    EditText etStation;
    @BindViews({R.id.et_IP1, R.id.et_IP2, R.id.et_IP3, R.id.et_IP4})
    List<EditText> mIPList;
    @BindViews({R.id.et_PORT1, R.id.et_PORT2, R.id.et_PORT3, R.id.et_PORT4})
    List<EditText> mPortList;
    @BindView(R.id.tv_switch)
    Button tvSwitch;
}
