package com.xplora.xpsettings.Activity;

import android.content.ComponentName;
import android.content.Intent;
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.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

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 DisplayActivity extends BaseActivity {
    private final List<IconProgressModel> mDataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();

        mDataList.clear();
        initData();

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

    private void initData() {
        int appNameType = DataManager.getAppNameType();
        int brightness = DataManager.getBrightness();

        String[] stringList = ResUtils.getStringArray("display_title_array_", 4);
        for (int i = 0; i < stringList.length; i++) {
            IconProgressModel model = new IconProgressModel();
            model.title = stringList[i].trim();
            if (i == 0) {
                model.icon = R.drawable.ic_brightness;
                model.progress = brightness;
                model.progressColor = R.color.xp_yellow;
                model.cellType = Constant.CellType.PROGRESS_TITLE;
            } else if (i == 1)  {
                model.cellType = Constant.CellType.SWITCH_FOOTER;
                model.isOn = i == appNameType;
                model.subTitle = getResources().getString(R.string.app_name_status_hint);
            } else if (i == 2) {
                model.icon = R.drawable.ic_watchface;
                model.cellType = Constant.CellType.ICON_TITLE_CENTER;
                model.isShowArrow = false;
            } else if (i == 3) {
                model.icon = R.drawable.ic_screen_sleep;
                model.cellType = Constant.CellType.ICON_TITLE_CENTER;
                model.isShowArrow = false;
            }
            mDataList.add(model);
        }
    }

    private void initView() {
        ListView listView = findViewById(R.id.activity_listview);
        mAdapter = new BaseListAdapter(this, R.layout.item_xpstyle, mDataList, mHandler);
        listView.setAdapter(mAdapter);
        listView.setDivider(new ColorDrawable(Color.TRANSPARENT));
        listView.setDividerHeight(1);
        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent;
            switch (i) {
                case 0:
                    intent = new Intent(this, ChangeProgressActivity.class);
                    intent.putExtra(Constant.INTENT_VIEW_TYPE, 3);
                    startActivityForResult(intent, 112);
                    break;
                case 2:
                    gotoWatchFace();
                    break;
                case 3:
                    intent = new Intent(this, ScreenTimeActivity.class);
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        });
    }

    public void gotoWatchFace() {
        Log.d(TAG, "gotoWatchFace: ");
        //该应用的包名
        String pkg = "com.xplora.xplauncher";
        //应用的主activity类
        String cls = pkg + ".activity.FaceSelectActivity";
        ComponentName componet = new ComponentName(pkg, cls);

        Intent intent = new Intent();
        intent.putExtra("intent_from", 0);
        intent.setComponent(componet);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 112 && resultCode == RESULT_OK) {
//            int value = data.getIntExtra(Constant.INTENT_RESULT_VALUE, 0);
//            IconProgressModel model = mDataList.get(0);
//            model.progress = 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_SWITCH_CHANGE) {
                int index = msg.arg1;
                int isOn = msg.arg2;
                IconProgressModel model = mDataList.get(index);
                model.isOn = isOn >= 1;
                DataManager.setAppNameType(isOn);
            }
        }
    };
}