package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.media.AudioManager;
import android.media.Ringtone;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class SoundModeActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_title);

        String[] array = ResUtils.getStringArray("settings_title_array_", 13);
        setWatchTitle(array[1]);

        initData();
        initView();
    }

    private void initData() {
        Ringtone ringTone = DataManager.getDefaultRingTone();
        String[] stringList = ResUtils.getStringArray("soundmode_title_array_", 3);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();
            if (i == 0){
                model.cellType = Constant.CellType.TITLE;
            } else if (i == 1){
                int isVibrate = DataManager.getIsVibrate();
                model.isOn = isVibrate >= 1;
                model.cellType = Constant.CellType.SWITCH_RIGHT;
            } else if (i == 2){
                model.cellType = Constant.CellType.TITLE_SUBTITLE;
                model.subTitle = ringTone != null ? ringTone.getTitle(this) : "";
            }
            mDataList.add(model);
        }
    }

    private void initView() {
        mAdapter = new BaseListAdapter(this, R.layout.item_xpstyle, mDataList, mHandler);
        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) {
                clickItem(position);
            }
        });
    }

    private void refreshView(int position) {
        int ringerMode = viewMode2RingerMode(position);
        DataManager.setRingerMode(ringerMode);

        mDataList.clear();
        initData();

        mAdapter.setBaseObjects(mDataList);
        mAdapter.notifyDataSetInvalidated();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void clickItem(int position) {
        switch (position) {
            case 0:
                Intent intent1 = new Intent(SoundModeActivity.this, VolumeActivity.class);
                startActivity(intent1);
                break;
            case 1:
                break;
            case 2:
                Intent intent2 = new Intent(SoundModeActivity.this, RingToneActivity.class);
                startActivityForResult(intent2, 112);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 112 && resultCode == RESULT_OK) {
            Ringtone ringTone = DataManager.getDefaultRingTone();
            BaseModel model = mDataList.get(2);
            model.subTitle = ringTone.getTitle(this);
            mAdapter.setBaseObjects(mDataList);
            mAdapter.notifyDataSetInvalidated();
        }
    }

    Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == Constant.MSG_SWITCH_CHANGE) {
                int index = msg.arg1;
                int isOn = msg.arg2;
                BaseModel model = mDataList.get(index);
                model.isOn = isOn >= 1;
                DataManager.setIsVibrate(isOn);
            }
        }
    };

    public int ringerMode2ViewMode(int ringerMode) {
        if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
            return 2;
        } else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
            return 1;
        } else {
            return 0;
        }
    }

    public int viewMode2RingerMode(int viewMode) {
        if (viewMode == 2) {
            return AudioManager.RINGER_MODE_SILENT;
        } else if (viewMode == 1) {
            return AudioManager.RINGER_MODE_VIBRATE;
        } else {
            return AudioManager.RINGER_MODE_NORMAL;
        }
    }
}