package com.xplora.xpsettings.Activity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

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

import java.util.concurrent.atomic.AtomicInteger;


public class ScreenTimeActivity extends BaseActionActivity {
    private int lastValue;
    private int initValue;
    private int[] timeValues;

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

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

    private void initData() {
        timeValues = getResources().getIntArray(R.array.screen_time_set_array);
        int curIndex = getCurrentIndex();
        String[] stringList = ResUtils.getStringArray("screen_time_value_array_", 5);
        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.setDivider(new ColorDrawable(Color.TRANSPARENT));
        listView.setDividerHeight(0);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                save(position);
                refresh(position);
            }
        });
    }

    public int getCurrentIndex() {
        int value = DataManager.getScreenTime();
        if (value == Integer.MAX_VALUE) {
            return timeValues.length-1; //size + 1,
        } else {
            int screentime = DataManager.getScreenTime() / 1000;
            for (int i = 0; i < timeValues.length; i++) {
                if (timeValues[i] == screentime) {
                    return i;
                }
            }
        }
        return -1;
    }

    public void save(int position) {
        if (position >= timeValues.length) {
            DataManager.setScreenTime(Integer.MAX_VALUE);
        } else {
            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();
    }
}