package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
//import android.os.SystemProperties;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemProperties;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

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

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


public class ApnListActivity extends BaseActivity {
    List<ApnModel> mApnList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        mApnList = ApnManager.getAPNList(getApplicationContext());
        initData();
        initView();
    }

    private void initData() {
        for (int i = 0; i < mApnList.size(); i++) {
            addBaseMode(mApnList.get(i));
        }
        addButtonMode();
    }

    private void addBaseMode(ApnModel apnModel) {
        String[] stringList = getResources().getStringArray(R.array.apn_titls_array);
        String apnName = apnModel.apn_name.trim();
        String mcc = stringList[3] + " : " + apnModel.mcc;
        String mnc = stringList[4] + " : " + apnModel.mnc;
        BaseModel model = new BaseModel();
        model.title = apnName;
        model.subTitle = mcc + "   " + mnc;
        model.isShowArrow = true;
        model.subColor = R.color.xp_yellow;
        model.cellType = Constant.CellType.TITLE_SUBTITLE;
        mDataList.add(model);
    }

    private void addButtonMode() {
        BaseModel model = new BaseModel();
        model.cellType = Constant.CellType.SURE;
        mDataList.add(model);
    }

    private void initView() {
        ListView listView = findViewById(R.id.activity_listview);
        View emptyView = findViewById(R.id.empty_view);

        listView.setVisibility(mApnList.size() == 0 ? View.GONE : View.VISIBLE);
        emptyView.setVisibility(mApnList.size() == 0 ? View.VISIBLE : View.GONE);

        initEmptyView();
        initList(listView);
    }

    private void initEmptyView() {
        TextView textView = findViewById(R.id.empty_text);
        textView.setText(R.string.apn_hint);

        Button button = findViewById(R.id.empty_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickAddButton();
            }
        });
    }

    private void initList(ListView listView) {
        mAdapter = new BaseListAdapter(this, 0, mDataList, mHandler);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ApnListActivity.this, ApnDeleteActivity.class);
                intent.putExtra(Constant.INENT_LIST_INDEX, position);
                startActivityForResult(intent, 0);
            }
        });
    }

    public void clickAddButton() {
        Intent intent = new Intent(ApnListActivity.this, ApnEditActivity.class);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null)
            return;
        if (data.getAction().equals(Constant.INENT_RESULT_ACTION)) {
            ApnModel apnModel = (ApnModel)data.getSerializableExtra(Constant.INENT_RESULT_VALUE);
            insertApn(apnModel);
        } else if (data.getAction().equals(Constant.INENT_RESULT_ACTION_REMOVE)) {
            int index = data.getIntExtra(Constant.INENT_RESULT_VALUE, -1);
            deleteApn(index);
        }
    }

    void insertApn(ApnModel apnModel) {
        ApnManager.addAPN(getApplicationContext(), apnModel.apn_name, apnModel.user_name, apnModel.password, apnModel.mcc, apnModel.mnc);

        mApnList.add(apnModel);
        mDataList.clear();
        initData();
        initView();
        mAdapter.setBaseObjects(mDataList);
        mAdapter.notifyDataSetInvalidated();
    }

    void deleteApn(int index) {
        if (index >= 0 && index <= mApnList.size()-1) {
            mApnList.remove(index);
            mDataList.clear();
            initData();
            initView();
            mAdapter.setBaseObjects(mDataList);
            mAdapter.notifyDataSetInvalidated();
        }
    }

    Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == Constant.MSG_SURE_CLICK) {
                clickAddButton();
            }
        }
    };
}