package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.content.res.Configuration;
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.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;


public class LanguageActivity extends BaseActivity {

    @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() {
        int curLocal = getCurrentIndex();
        String[] stringList = getResources().getStringArray(R.array.language_title_array);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();
            model.isOn = i == curLocal;
            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);
                reboot();
            }
        });
    }

    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 void save(int position) {
        String[] localList = getResources().getStringArray(R.array.language_Local_array);
        //DataManager.setLanguage(localList[position]);
        Locale locale = new Locale(localList[position]);
        updateLanguage(locale);
    }

    public int getCurrentIndex() {
        String curLocal = DataManager.getLanguage(getApplicationContext());
        String[] localList = getResources().getStringArray(R.array.language_Local_array);
        for (int i = 0; i < localList.length; i++) {
            if (localList[i].equals(curLocal)) {
                return i;
            }
        }
        return -1;
    }

    public void updateLanguage(Locale locale) {
        try {
            Object objIActMag, objActMagNative;
            Class clzIActMag = Class.forName("android.app.IActivityManager");
            Class clzActMagNative = Class
                    .forName("android.app.ActivityManagerNative");
            Method mtdActMagNative$getDefault = clzActMagNative
                    .getDeclaredMethod("getDefault");
            objIActMag = mtdActMagNative$getDefault.invoke(clzActMagNative);
            Method mtdIActMag$getConfiguration = clzIActMag
                    .getDeclaredMethod("getConfiguration");
            Configuration config = (Configuration) mtdIActMag$getConfiguration
                    .invoke(objIActMag);
            config.locale = locale;
            Class clzConfig = Class
                    .forName("android.content.res.Configuration");
            java.lang.reflect.Field userSetLocale = clzConfig
                    .getField("userSetLocale");
            userSetLocale.set(config, true);
            Class[] clzParams = { Configuration.class };
            Method mtdIActMag$updateConfiguration = clzIActMag
                    .getDeclaredMethod("updateConfiguration", clzParams);
            mtdIActMag$updateConfiguration.invoke(objIActMag, config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void reboot() {
        if (Constant.DEBUG) {
            return;
        }
        Intent intent2 = new Intent(Intent.ACTION_REBOOT);
        intent2.putExtra("nowait ", 1);
        intent2.putExtra("interval ", 1);
        intent2.putExtra("window ", 0);
        sendBroadcast(intent2);
    }
}