package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
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.BaseModel;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Utils.Constant;
import com.xplora.xpsettings.Utils.ResUtils;


public class TelephoneActivity extends BaseActionActivity {
    public BaseListAdapter adapter;

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

    private void initData() {
        String[] stringList = ResUtils.getStringArray("telephone_title_array_", 3);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();
            model.cellType = Constant.CellType.SWITCH_RIGHT;
            model.isOn = getActionStatus(ACTION_TYPE.VOLTE.ordinal() + i) > 0;
            model.isShowArrow = false;
            mDataList.add(model);
        }
    }

    private void initView() {
        adapter = new BaseListAdapter(TelephoneActivity.this, 0, mDataList, mHandler);
        ListView listView = findViewById(R.id.activity_listview);
        listView.setAdapter(adapter);
        listView.setDivider(new ColorDrawable(Color.TRANSPARENT));
        listView.setDividerHeight(0);
        listView.setOnItemClickListener((adapterView, view, i, l) -> {});
    }

    Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == Constant.MSG_SWITCH_CHANGE) {
                int index = msg.arg1;
                int isOn = msg.arg2;
                BaseModel model = mDataList.get(index);
                model.isOn = isOn >= 1;
                setActionStatus(ACTION_TYPE.VOLTE.ordinal()+ index, isOn);
            }
        }
    };
}