package com.xplora.xpsettings.Activity;

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 ScreenTimeActivity extends BaseActivity {
    private int lastValue;
    private int initValue;
    private int[] timeValues;

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

    @Override
    protected void onPause() {
        super.onPause();
        if (lastValue != initValue) {
            DataManager.sendLocalSocketIntent(DataManager.ACTION_SCREEN_TIME_CHANGED, lastValue);
        }
    }

    private void initData() {
        int curIndex = getCurrentIndex();
        String[] stringList = getResources().getStringArray(R.array.screen_time_value_array);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();
            model.isOn = i == curIndex;
            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) {
                save(position);
                refresh(position);
            }
        });
    }

    public void save(int position) {
        AtomicInteger time = new AtomicInteger(10 * 1000);
        time.set(timeValues[position] * 1000);
        lastValue = time.intValue();
        DataManager.setScreenTime(lastValue);
    }

    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();
    }

    public int getCurrentIndex() {
        timeValues = getResources().getIntArray(R.array.screen_time_set_array);
        int screentime = DataManager.getScreenTime() / 1000;
        for (int i = 0; i < timeValues.length; i++) {
            if (timeValues[i] == screentime) {
                return i;
            }
        }
        return -1;
    }
}