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.widget.ListView;

import androidx.annotation.NonNull;

import com.xplora.xpsettings.Adapter.BaseListAdapter;
import com.xplora.xpsettings.Data.DataManager;
import com.xplora.xpsettings.Model.ApnModel;
import com.xplora.xpsettings.Model.BaseModel;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Utils.Constant;

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

public class ApnEditActivity extends BaseActivity {
    public ApnModel mApnModel = new ApnModel();

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

    private void initData() {
        String[] stringList = getResources().getStringArray(R.array.apn_titls_array);
        for (String s: stringList) {
            BaseModel model = new BaseModel();
            model.title = s.trim();
            model.isShowArrow = false;
            model.cellType = Constant.CellType.TITLE_EDIT;
            mDataList.add(model);
        }

        //底部加按钮
        BaseModel model = new BaseModel();
        model.cellType = Constant.CellType.CANCEL_SURE;
        mDataList.add(model);

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

    private void initView() {
        mAdapter = new BaseListAdapter(ApnEditActivity.this, R.layout.item_xpstyle, mDataList, mHandler);
        ListView listView = findViewById(R.id.activity_listview);
        listView.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;
                switch (msg.arg1) {
                    case 0:
                        mApnModel.apn_name = s.trim();
                        break;
                    case 1:
                        mApnModel.user_name = s.trim();
                        break;
                    case 2:
                        mApnModel.password = s.trim();
                        break;
                    case 3:
                        mApnModel.mcc = s.trim();
                        break;
                    case 4:
                        mApnModel.mnc = s.trim();
                        break;
                    default:
                        break;
                }
                mDataList.get(msg.arg1).value = s.trim();
                mAdapter.setBaseObjects(mDataList);
            } else if (msg.what == Constant.MSG_CANCEL_CLICK) {
                Intent intent = new Intent();
                intent.setAction(Constant.INENT_RESULT_ACTION_CANCEL);
                setResult(RESULT_OK, intent);
                finish();
            } else if (msg.what == Constant.MSG_SURE_CLICK) {
                Intent intent = new Intent();
                intent.setAction(Constant.INENT_RESULT_ACTION);
                intent.putExtra(Constant.INENT_RESULT_VALUE, mApnModel);
                setResult(RESULT_OK, intent);
                finish();
            }
        }
    };

    @Override
    public void onBackPressed() {
        boolean isKeyboard = mAdapter.isKeyboardVisible();
        if (isKeyboard) {
            mAdapter.closeKeyboard();
        } else {
            super.onBackPressed();
        }
    }
}
