package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.inputmethod.EditorInfo;
import android.widget.AbsListView;
import android.widget.ListView;

import androidx.annotation.NonNull;

import com.xplora.xpsettings.Adapter.BaseListAdapter;
import com.xplora.xpsettings.Observer.Listener;
import com.xplora.xpsettings.Model.ApnModel;
import com.xplora.xpsettings.Model.BaseModel;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Utils.Constant;
import com.xplora.xpsettings.Utils.ResUtils;

public class ApnEditActivity extends BaseActionActivity implements Listener.ListViewListener {
    public ApnModel mApnModel = new ApnModel();
    private ListView mListView = null;
    public int mType = 0; // 0：edit  1 : add

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_edit);
        initIntent();
        initData();
        initView();
    }

    private void initIntent() {
        mType = getIntent().getIntExtra(Constant.INTENT_VIEW_VALUE, 0);
        if (mType == 0) { //如果是编辑状态
            mApnModel = (ApnModel) getIntent().getSerializableExtra(Constant.INTENT_RESULT_VALUE);

            //如果是特定的apn，设置默认值
            String[] specifiedValues = ResUtils.getStringArray("apn_value_array_", 9);
            Log.d(TAG, "initIntent: " + specifiedValues.length);
            if (specifiedValues.length < 9)
                return;
            boolean isNameEquals = mApnModel.name.equals(specifiedValues[0]);
            boolean isMccEquals = mApnModel.mcc.equals(specifiedValues[1]);
            boolean isMncEquals = mApnModel.mnc.equals(specifiedValues[2]);
            if (isNameEquals && isMccEquals && isMncEquals) {
                mApnModel.apn_name = specifiedValues[3];
                mApnModel.auth_type = specifiedValues[4];
                mApnModel.apn_type = specifiedValues[5];
                mApnModel.apn_protocol = specifiedValues[6];
                mApnModel.apn_roaming_protocol = specifiedValues[7];
                mApnModel.bearer = specifiedValues[8];
            }
        }
    }

    private void initData() {
        String[] stringList = ResUtils.getStringArray("apn_title_array_", 19);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();
            model.isShowArrow = false;
            model.cellType = Constant.CellType.TITLE_EDIT;
            model.editMaxWidth = (i == 1 || i == 2) ? 3 : Integer.MAX_VALUE;
            model.editInputType = (i == 1 || i == 2) ? EditorInfo.TYPE_CLASS_NUMBER : EditorInfo.TYPE_CLASS_TEXT;
            model.value = getApnModelValue(i);
            model.isEditTextEnable = !mApnModel.isCurrentApn;
            mDataList.add(model);
        }

        //当前正在使用的vpn不能编辑
        if (!mApnModel.isCurrentApn) {
            //底部加按钮
            BaseModel model = new BaseModel();
            model.cellType = Constant.CellType.CANCEL_SURE;
            mDataList.add(model);

            BaseModel model1 = new BaseModel();
            model1.cellType = Constant.CellType.EMPTY2;
            mDataList.add(model1);
        }
    }

    private void initView() {
        mAdapter = new BaseListAdapter(ApnEditActivity.this, R.layout.item_xpstyle, mDataList, mHandler);
        mListView = findViewById(R.id.activity_listview);
        mListView.setAdapter(mAdapter);
    }

    Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == Constant.MSG_TEXTEDIT_FINISH) {
                String s = (String) msg.obj;
                changeApnModel(msg.arg1, s);
                mDataList.get(msg.arg1).value = s.trim();
            } else if (msg.what == Constant.MSG_CANCEL_CLICK) {
                Intent intent = new Intent();
                intent.setAction(Constant.INTENT_RESULT_ACTION_CANCEL);
                setResult(RESULT_OK, intent);
                finish();
            } else if (msg.what == Constant.MSG_SURE_CLICK) {
                if (!check())
                    return;
                Intent intent = new Intent();
                intent.setAction(Constant.INTENT_RESULT_ACTION);
                intent.putExtra(Constant.INTENT_VIEW_VALUE, mType);
                intent.putExtra(Constant.INTENT_RESULT_VALUE, mApnModel);
                setResult(RESULT_OK, intent);
                finish();
            }
        }
    };

    public void changeApnModel(int index, String s) {
        switch (index) {
            case 0:
                mApnModel.name = s.trim();
                break;
            case 1:
                mApnModel.mcc = s.trim();
                break;
            case 2:
                mApnModel.mnc = s.trim();
                break;
            case 3:
                mApnModel.apn_name = s.trim();
                break;
            case 4:
                mApnModel.user_name = s.trim();
                break;
            case 5:
                mApnModel.password = s.trim();
                break;
            case 6:
                mApnModel.proxy = s.trim();
                break;
            case 7:
                mApnModel.port = s.trim();
                break;
            case 8:
                mApnModel.server = s.trim();
                break;
            case 9:
                mApnModel.mmsc = s.trim();
                break;
            case 10:
                mApnModel.mmsproxy = s.trim();
                break;
            case 11:
                mApnModel.mmsport = s.trim();
                break;
            case 12:
                mApnModel.auth_type = s.trim();
                break;
            case 13:
                mApnModel.apn_type = s.trim();
                break;
            case 14:
                mApnModel.apn_protocol = s.trim();
                break;
            case 15:
                mApnModel.apn_roaming_protocol = s.trim();
                break;
            case 16:
                mApnModel.bearer = s.trim();
                break;
            case 17:
                mApnModel.mvno_type = s.trim();
                break;
            case 18:
                mApnModel.mvno_match_data = s.trim();
                break;
            default:
                break;
        }
    }

    public String getApnModelValue(int index) {
        switch (index) {
            case 0:
                return mApnModel.name;
            case 1:
                return mApnModel.mcc;
            case 2:
                return mApnModel.mnc;
            case 3:
                return mApnModel.apn_name;
            case 4:
                return mApnModel.user_name;
            case 5:
                return mApnModel.password;
            case 6:
                return mApnModel.proxy;
            case 7:
                return mApnModel.port;
            case 8:
                return mApnModel.server;
            case 9:
                return mApnModel.mmsc;
            case 10:
                return mApnModel.mmsproxy;
            case 11:
                return mApnModel.mmsport;
            case 12:
                return mApnModel.auth_type;
            case 13:
                return mApnModel.apn_type;
            case 14:
                return mApnModel.apn_protocol;
            case 15:
                return mApnModel.apn_roaming_protocol;
            case 16:
                return mApnModel.bearer;
            case 17:
                return mApnModel.mvno_type;
            case 18:
                return mApnModel.mvno_match_data;
            default:
                return "";
        }
    }

    @Override
    public void onListClickPosition(int position) {
        mListView.setSelection(position);
    }

    private boolean check() {
        if (mApnModel.name.isEmpty()) {
            toast(getString(R.string.input_name));
            return false;
        }
        return true;
    }
}
