package kr.co.icontrols.iotplatform;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import kr.co.icontrols.iotplatform.DataClass.DeviceInfo;
import kr.co.icontrols.iotplatform.DataClass.UserInfo;
import kr.co.icontrols.iotplatform.Database.DBUtil;
import kr.co.icontrols.iotplatform.DragListView.ChangeListener;
import kr.co.icontrols.iotplatform.DragListView.RankListAdapter;
import kr.co.icontrols.iotplatform.DragListView.SwipeAndDragHelper;

import static kr.co.icontrols.iotplatform.Database.DatabaseHelper.TABLE_DEVICE_RANK;
import static kr.co.icontrols.iotplatform.Database.DatabaseHelper.TABLE_SMART_LIGHT;
import static kr.co.icontrols.iotplatform.utils.CommonUtils.showNotiMsg;
import static kr.co.icontrols.iotplatform.utils.Constants.EXTRA_DEVICE_LIST;

public class DeviceChangeRankActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = "DeviceChangeRankActivity";

    private String mMode;
    private ArrayList<DeviceInfo> mDeviceList;

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

        mMode = getIntent().getStringExtra("MODE");
        mDeviceList = (ArrayList<DeviceInfo>) getIntent().getSerializableExtra(EXTRA_DEVICE_LIST);

        RecyclerView itemRecyclerView = findViewById(R.id.recyclerview_device_list);
        itemRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        RankListAdapter adapter = new RankListAdapter(new ChangeListener() {
            @Override
            public void onChangeData() {
                findViewById(R.id.btnBack).setVisibility(View.GONE);
                findViewById(R.id.btnBackText).setVisibility(View.VISIBLE);
                findViewById(R.id.btnRight).setVisibility(View.VISIBLE);
            }
        });
        adapter.setDeviceInfoList(mDeviceList);
        SwipeAndDragHelper swipeAndDragHelper = new SwipeAndDragHelper(adapter);
        ItemTouchHelper touchHelper = new ItemTouchHelper(swipeAndDragHelper);
        adapter.setTouchHelper(touchHelper);
        itemRecyclerView.setAdapter(adapter);
        touchHelper.attachToRecyclerView(itemRecyclerView);
    }

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

    @Override
    public void onClick(View v) {
        /*
        final View fv = v;
        fv.setEnabled(false);
        Timer buttonTimer = new Timer();
        buttonTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            fv.setEnabled(true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {

                        }
                    }
                });
            }
        }, 500);
        */

        switch (v.getId()) {
            case R.id.btnAlarmSetting:
                startActivity(new Intent(this, AlarmSettingActivity.class));
                break;
            case R.id.btnAppInfo:
                startActivity(new Intent(this, AppInfoActivity.class));
                break;
            case R.id.btnBack:
            case R.id.btnBackText:
                onBackPressed();
                break;
            case R.id.btnRight:
                //인덱스 변경
                for(int i = 0 ; i < mDeviceList.size() ; i++) {
                    if(mMode.equals("GROUP_RANK")) {
                        mDeviceList.get(i).groupRank = i;
                    } else {
                        mDeviceList.get(i).rank = i;
                    }

                    if(mMode.equals("SMART_LIGHT")) {
                        DBUtil.updateDeviceItem(TABLE_SMART_LIGHT, mDeviceList.get(i));
                    } else if(mMode.equals("DEVICE_RANK") || mMode.equals("GROUP_RANK")) {
                        DBUtil.updateDeviceItem(TABLE_DEVICE_RANK, mDeviceList.get(i));
                    }
                }

                

                if(mMode.equals("SMART_LIGHT")) {
                    showNotiMsg(DeviceChangeRankActivity.this, "스마트 조명 순서를 변경하였습니다.", true);
                } else if(mMode.equals("DEVICE_RANK")) {
                    sortDeviceRank();
                    showNotiMsg(DeviceChangeRankActivity.this, "스마트 조명 순서를 변경하였습니다.", true);
                } else if(mMode.equals("GROUP_RANK")) {
                    sortGroupRank();
                    showNotiMsg(DeviceChangeRankActivity.this, "스마트 조명 순서를 변경하였습니다.", true);
                }
                break;
        }
    }

    /**
     * UserInfo.devices 순서 재정렬
     */
    private void sortDeviceRank() {
        for(DeviceInfo d1 : mDeviceList) {
            for(int i = 0 ; i < UserInfo.devices.size() ; i++) {
                if(d1.deviceId.equals(UserInfo.devices.get(i).deviceId)) {
                    UserInfo.devices.get(i).rank = d1.rank;
                }
            }
        }

        if(UserInfo.devices.size() > 1) {
            for (int i = 0; i < UserInfo.devices.size() - 1; i++) {
                for (int j = i + 1; j < UserInfo.devices.size(); j++) {
                    if (UserInfo.devices.get(i).rank > UserInfo.devices.get(j).rank) {
                        DeviceInfo item = UserInfo.devices.get(j);
                        UserInfo.devices.remove(j);
                        UserInfo.devices.add(i, item);
                    }
                }
            }
        }
    }

    private void sortGroupRank() {
        for(DeviceInfo d1 : mDeviceList) {
            for(int i = 0 ; i < UserInfo.devices.size() ; i++) {
                if(d1.deviceId.equals(UserInfo.devices.get(i).deviceId)) {
                    UserInfo.devices.get(i).groupRank = d1.groupRank;
                }
            }
        }
    }

}
