package com.usai.redant.raimage;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.usai.redant.raimage.Model.ModelActivity;
import com.usai.redant.raimage.POP.PopActivity;
import com.usai.util.Network;

import org.json.JSONArray;
import org.json.JSONObject;

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

public class ModeActivity extends AppCompatActivity {

    private ListView mode_list;
    private Button upload_setting_btn;
    private ImageView company_icon_iv;
    private TextView company_name_tv;
    private List<JSONObject> dataSource;
    private ModeAdapter adapter;
    /** Message Handler */
    private Handler msgHanlder = new Handler() {

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1024:{
                    company_icon_iv.setImageBitmap((Bitmap)msg.obj);
                }
            }
        }

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

        /**获取控件*/
        mode_list = (ListView)findViewById(R.id.mode_list);
        upload_setting_btn = (Button)findViewById(R.id.upload_setting_btn);
        company_icon_iv = (ImageView)findViewById(R.id.company_name_icon);
        company_name_tv = (TextView)findViewById(R.id.company_name_text);

        /**设置数据*/
        try {

            // company
            final String icon_link = RedAntApplication.server_info.getString("company_icon");
            String company_name = RedAntApplication.server_info.getString("company_name");

            if (!TextUtils.isEmpty(company_name)) {
                company_name_tv.setText(company_name);
            }

            if (!TextUtils.isEmpty(icon_link)) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // http://192.168.0.130/bo.png
                        Bitmap img = Network.getImageFromLink(icon_link);
                        if (img != null) {
                            Message msg = new Message();
                            msg.what = 1024;
                            msg.obj = img;
                            msgHanlder.sendMessage(msg);
                        }
                    }
                }).start();
            }

            // mode list
            JSONArray modeArr = RedAntApplication.server_info.getJSONArray("modellist");
            if (modeArr.length() > 0) {
                dataSource = new ArrayList<JSONObject>();
                for (int i = 0; i < modeArr.length();i++) {
                    JSONObject obj = modeArr.getJSONObject(i);
                    dataSource.add(obj);
                }
                adapter = new ModeAdapter(this,R.layout.mode_cell,dataSource);
                mode_list.setAdapter(adapter);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        /**List View 点击事件*/
        mode_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                JSONObject mode = dataSource.get(position);
               try {
                   int enable = mode.getInt("enable");
//                   if (enable != 0) {
                       showActivityMode(mode);
//                   }

               } catch (Exception e) {
                   e.printStackTrace();
               }

            }
        });

        /**上传设置按钮点击*/
        upload_setting_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showUploadSetting();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.logout_menu,menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.logout_item: {
//                Log.d("Mode List", "Menu Item Click " + item.getTitle());
                logout();
            }
                break;
        }
        return true;
    }

    /***/
    void showActivityMode(JSONObject mode) {

       try {
           String name = mode.getString("name");

           if (name.equals("POP")) {
               Intent intent = new Intent(this,PopActivity.class);
               startActivity(intent);
           } else if (name.equals("Model")) {
               Intent intent = new Intent(this,ModelActivity.class);
               startActivity(intent);
           }
       } catch (Exception e) {
           e.printStackTrace();
       }

    }

    /**上传设置*/
    void showUploadSetting() {
        Intent intent = new Intent(ModeActivity.this,uploadSettingActivity.class);
        startActivity(intent);
    }

    void logout() {
        RedAntApplication.user = null;
        RedAntApplication.password = null;
        RedAntApplication.server_info = null;
        Intent intent = new Intent(ModeActivity.this,FullScreenLoginActivity.class);
        startActivity(intent);
        finish();
    }

    /**数据源*/
    class ModeAdapter extends ArrayAdapter<JSONObject> {
        private int resourceID;
        public ModeAdapter(Context ctx, int resourceId, List<JSONObject> list) {
            super(ctx,resourceId,list);
            resourceID = resourceId;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            JSONObject mode = getItem(position);
            ViewHolder holder = null;
            View cell;
            if (convertView == null) {
                cell = LayoutInflater.from(getContext()).inflate(resourceID,null);
                holder = new ViewHolder();
                holder.mode = (TextView) cell.findViewById(R.id.mode_name_tv);
                cell.setTag(holder);
            } else  {
                cell = convertView;
                holder = (ViewHolder)cell.getTag();
            }

            try {
                holder.mode.setText(mode.getString("name").toString());
                int enable = mode.getInt("enable");

                if (enable == 0) {
                    cell.setBackgroundColor(getResources().getColor(R.color.dark_gray));
                } else  {
                    cell.setBackgroundColor(getResources().getColor(R.color.clear_color));
                }
                return cell;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        class ViewHolder {
            TextView mode;
        }
    }
}
