package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Switch;

import androidx.annotation.NonNull;

import com.xplora.xpsettings.Adapter.BaseListAdapter;
import com.xplora.xpsettings.Adapter.ListAdapter;
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.concurrent.atomic.AtomicInteger;


public class RingToneActivity extends BaseActivity {
    public int mRingToneIndex = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        initData();
        initView();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void initData() {
        mRingToneIndex = DataManager.getRingTone();

        String[] stringList = getResources().getStringArray(R.array.ringtone_title_array);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();
            model.isOn = mRingToneIndex == i;
            model.cellType = Constant.CellType.CHECKBOX;
            mDataList.add(model);
        }
    }

    private void initView() {
        mAdapter = new BaseListAdapter(this, 0, mDataList);
        ListView listView = findViewById(R.id.activity_listview);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mRingToneIndex = position;
                save(position);
                refresh(position);
            }
        });
    }

    public void save(int position) {
        DataManager.setRingTone(position);
    }

    public void refresh(int position) {
        for (int i = 0; i < mDataList.size(); i++) {
            BaseModel model = mDataList.get(i);
            model.isOn = position == i;
        }
        mAdapter.setBaseObjects(mDataList);
        mAdapter.notifyDataSetInvalidated();
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.setAction(Constant.INENT_RESULT_ACTION);
        intent.putExtra(Constant.INENT_RESULT_VALUE, mRingToneIndex);
        setResult(RESULT_OK, intent);
        finish();
    }
}