package com.xplora.xpsettings.Activity;

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.provider.Settings;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

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;

import java.util.ArrayList;
import java.util.List;
import android.content.Intent;

import androidx.annotation.NonNull;

public class TimeFormatActivity extends BaseActionActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_title_center);

        //标题
        String[] array = ResUtils.getStringArray("settings_title_array_", 13);
        setWatchTitle(array[4]);

        initData();
        initView();
    }

    private void initData() {
        String timeFormat = DataManager.getTimeFormat();
        int index = timeFormat.equals("12") ? 0 : 1;
        String[] stringList = ResUtils.getStringArray("timeformat_title_array_", 2);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();
            model.isOn = i == index;
            model.titileColor = R.color.xp_yellow;
            model.cellType = Constant.CellType.TITLE_CENTER;
            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) {
                changeTimeFormat(position);

                //界面
                for (int i = 0; i < mDataList.size(); i++) {
                    BaseModel model = mDataList.get(i);
                    model.isOn = position == i;
                }
                mAdapter.setBaseObjects(mDataList);
                mAdapter.notifyDataSetInvalidated();
            }
        });
    }

    private void changeTimeFormat(int index) {
        DataManager.setTimeFormat(index == 0 ? Constant.TIMEFORMAT_12 : Constant.TIMEFORMAT_24);
        Intent intent=new Intent("action.update.clockformat");
        intent.setPackage("com.android.systemui");
        sendBroadcast(intent);
    }
}