package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.media.AudioManager;
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.ResUtils;

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


public class SoundModeActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_title);
        setWatchTitle(getString(R.string.sound_mode));
        initData();
        initView();
    }

    private void initData() {
        int ringerMode = DataManager.getRingerMode(getApplicationContext());
        int soundType = ringerMode2ViewMode(ringerMode);
        int isVibrate = DataManager.getIsVibrate();
        int ringTone = DataManager.getRingTone();
        String[] ringToneList = getResources().getStringArray(R.array.ringtone_title_array);

        String[] stringList = getResources().getStringArray(R.array.soundmode_title_array);
        for (int i = 0; i < stringList.length; i++) {
            BaseModel model = new BaseModel();
            model.title = stringList[i].trim();
            if (i <= 2) {
                model.cellType = Constant.CellType.CHECKBOX;
                model.isOn = soundType == i;
            } else if (i == 3){
                model.cellType = Constant.CellType.SWITCH;
                model.isOn = isVibrate >= 1;
            }  else if (i == 4){
                model.cellType = Constant.CellType.TITLE;
            }  else if (i == 5){
                model.cellType = Constant.CellType.TITLE_SUBTITLE;
                model.subTitle = ringToneList[ringTone];
            }
            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);
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void clickItem(int position) {
        switch (position) {
            case 0: case 1: {
                int ringerMode = viewMode2RingerMode(position);
                DataManager.setRingerMode(ringerMode);
                for (int i = 0; i < 3; i++) {
                    BaseModel model = mDataList.get(i);
                    model.isOn = position == i;
                }
                mAdapter.setBaseObjects(mDataList);
                mAdapter.notifyDataSetInvalidated();
                break;
            }
            case 2: {
                //获取Do not disturb权限,才可进行音量操作
                if (!DataManager.hasSilentPermission()) {
                    Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
                    startActivity(intent);
                } else {
                    int ringerMode = viewMode2RingerMode(position);
                    DataManager.setRingerMode(ringerMode);
                    for (int i = 0; i < 3; i++) {
                        BaseModel model = mDataList.get(i);
                        model.isOn = position == i;
                    }
                    mAdapter.setBaseObjects(mDataList);
                    mAdapter.notifyDataSetInvalidated();
                }
                break;
            }
            case 3:
                break;
            case 4:
                Intent intent1 = new Intent(SoundModeActivity.this, VolumeActivity.class);
                startActivity(intent1);
                break;
            case 5:
                Intent intent2 = new Intent(SoundModeActivity.this, RingToneActivity.class);
                startActivityForResult(intent2, 0);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null)
            return;
        if (data.getAction().equals(Constant.INENT_RESULT_ACTION)) {
            int value = data.getIntExtra(Constant.INENT_RESULT_VALUE, 0);
            String[] ringToneList = getResources().getStringArray(R.array.ringtone_title_array);

            BaseModel model = mDataList.get(5);
            model.subTitle = ringToneList[value];
            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_SWITCHC_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;
        }
    }
}